Currently, the below chart is being created using JFreeChart library.
The code to generate the above image is as follows:
However, even though, I set the plot as black color using: plot.getRenderer().setSeriesPaint(2,new Color(0, 0, 0) );
The graph always appears red.
// using JFreeChart // create a dataset... DefaultCategoryDataset dataset = new DefaultCategoryDataset(); String series1 = "day"; dataset.addValue(5.0, series1, "1"); dataset.addValue(4.0, series1, "2"); dataset.addValue(4.5, series1, "3"); dataset.addValue(4.3, series1, "4"); dataset.addValue(4.0, series1, "5"); dataset.addValue(3.1, series1, "6"); dataset.addValue(3.2, series1, "7"); dataset.addValue(3.2, series1, "8"); dataset.addValue(2.0, series1, "9"); series1 = "night"; dataset.addValue(5.0, series1, "10"); dataset.addValue(4.0, series1, "11"); dataset.addValue(4.5, series1, "12"); dataset.addValue(4.3, series1, "13"); dataset.addValue(4.0, series1, "14"); dataset.addValue(3.1, series1, "15"); // create the first renderer... final CategoryItemRenderer areaRenderer = new AreaRenderer(); final CategoryPlot plot = new CategoryPlot(); plot.setDataset(dataset); plot.setRenderer(areaRenderer); //hide the gridlines plot.setDomainGridlinesVisible(false); plot.setRangeGridlinesVisible(false); //hide the xAxis plot.setDomainAxis(new CategoryAxis("")); plot.getDomainAxis().setVisible(false); plot.getDomainAxis().setCategoryMargin(0); // hide the margin between the x-axis //hide the yAxis plot.setRangeAxis(new NumberAxis("")); plot.getRangeAxis().setVisible(false); // set the orientation plot.setOrientation(PlotOrientation.VERTICAL); // // create the second dataset and renderer... series1 = "lastYear"; plot.setRangeAxis(new NumberAxis("")); plot.getRangeAxis().setVisible(false); DefaultCategoryDataset dataset2 = new DefaultCategoryDataset(); dataset2.addValue(6.0, series1, "1"); dataset2.addValue(5.0, series1, "2"); dataset2.addValue(5.5, series1, "3"); dataset2.addValue(4.3, series1, "4"); dataset2.addValue(3.0, series1, "5"); dataset2.addValue(3.1, series1, "6"); dataset2.addValue(5.2, series1, "7"); dataset2.addValue(6.2, series1, "8"); dataset2.addValue(2.0, series1, "9"); dataset2.addValue(4.0, series1, "10"); dataset2.addValue(4.0, series1, "11"); dataset2.addValue(4.5, series1, "12"); dataset2.addValue(4.3, series1, "13"); dataset2.addValue(3.0, series1, "14"); dataset2.addValue(5.1, series1, "15"); final CategoryItemRenderer lineAndShapeRenderer = new LineAndShapeRenderer(); plot.setDataset(2, dataset2); plot.setRenderer(2, lineAndShapeRenderer); // set the color plot.getRenderer().setSeriesPaint(0,new Color(0, 182, 125) ); plot.getRenderer().setSeriesPaint(1,new Color(182, 224, 238)); plot.getRenderer().setSeriesPaint(2,new Color(0, 0, 0) ); plot.getRenderer().setSeriesShape(2, ShapeUtilities.createDiamond(5)); // plot.mapDatasetToRangeAxis(2, 1); // change the rendering order so the primary dataset appears "behind" the plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); final JFreeChart chart = new JFreeChart(plot); //hide the legend chart.removeLegend(); ChartUtilities.saveChartAsPNG(new File("./report-template/chart/sample.png"), chart, 400, 300);
How do I change the color from red to black and how do I remove/hide the “square data points”, such that it becomes a simple line graph?
Advertisement
Answer
Note that CategoryPlot::getRenderer
, without an index parameter, returns the first renderer. As there there is no series with index 2
, use the renderer directly to alter its series for index 0
:
//plot.getRenderer().setSeriesPaint(2, new Color(0, 0, 0)); lineAndShapeRenderer.setSeriesPaint(0, new Color(0, 0, 0));
To hide the shapes, as shown here and noting the API change, you could use either of these on your LineAndShapeRenderer
instance:
//CategoryItemRenderer areaRenderer = new AreaRenderer(); LineAndShapeRenderer lineAndShapeRenderer = new LineAndShapeRenderer(); … lineAndShapeRenderer.setSeriesShapesVisible(0, false); lineAndShapeRenderer.setDefaultShapesVisible(false);