Here is an example; it sets the render like this:
final XYPlot plot = xylineChart.getXYPlot( );
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
renderer.setSeriesPaint( 0 , Color.RED );
renderer.setSeriesPaint( 1 , Color.GREEN );
renderer.setSeriesPaint( 2 , Color.YELLOW );
renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
renderer.setSeriesStroke( 1 , new BasicStroke( 3.0f ) );
renderer.setSeriesStroke( 2 , new BasicStroke( 2.0f ) );
plot.setRenderer( renderer );
And the data set like this:
final XYSeries firefox = new XYSeries( "Firefox" );
firefox.add( 1.0 , 1.0 );
firefox.add( 2.0 , 4.0 );
firefox.add( 3.0 , 3.0 );
final XYSeries chrome = new XYSeries( "Chrome" );
chrome.add( 1.0 , 4.0 );
chrome.add( 2.0 , 5.0 );
chrome.add( 3.0 , 6.0 );
final XYSeries iexplorer = new XYSeries( "InternetExplorer" );
iexplorer.add( 3.0 , 4.0 );
iexplorer.add( 4.0 , 5.0 );
iexplorer.add( 5.0 , 4.0 );
final XYSeriesCollection dataset = new XYSeriesCollection( );
dataset.addSeries( firefox );
dataset.addSeries( chrome );
dataset.addSeries( iexplorer );
Then the result is like this:
I can not see any code to give each data series a unique graph, but the result gives the three kinds of data the graph set before, such firefox is red, chrome is green.
Is the graph assigned in order?
If so, if I add another data to the dataset, which colour it will be assigned?
If not, how to connect the graph with the data? What if I want to give firefox green and chrome red?
Thank you very much!
Is the graph assigned in order?
An "XYPlot
makes use of an XYItemRenderer
to draw each point on the plot." This includes all points in all series in the plot's XYDataset
. Your example's dataset includes three series, each having three points; moreover, each point in a series is connected by a line, as your image illustrates. The order within an XYSeries
is sorted by default, as shown here.
If so, if I add another data to the dataset, which colour it will be assigned? If not, how to connect the graph with the data? What if I want to give firefox green and chrome red?
Absent an explicit call to setSeriesPaint()
, the next color is supplied by the plot's DrawingSupplier
, discussed here. The choice may be influenced by your use case: for example, a custom DrawingSupplier
is warranted if the chart's plot and legend should match in a particular way, as shown here and here.