Line Dashed

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
sergio_barcelos
Posts: 5
Joined: Thu May 06, 2021 6:51 pm
antibot: No, of course not.

Line Dashed

Post by sergio_barcelos » Sat Jun 12, 2021 3:46 am

Hi,
I'm trying to compose a chart in JFreeChart with the following features:
- Generate two series, two axes type numbers;
- One of the series must be in black color with a solid line;
- The other should be with a black dashed line.
I'm not able to do both features together. Either it's dashed with the default colors, or it's black without the dash. Can someone help me?
Grateful for the attention.

NOTE
This code is adapted from demo codes.

<CODE>
package br.com.linhaSeries.aplicacao;

import java.awt.BasicStroke;
import java.awt.Color;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.chart.ui.UIUtils;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class LineChartDemo4 extends ApplicationFrame {

public LineChartDemo4(String title) {

super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(600, 400));
setContentPane(chartPanel);

}

private static XYDataset createDataset() {

XYSeries series1 = new XYSeries("First");
series1.add(1.0, 1.0);
series1.add(2.0, 4.0);
series1.add(3.0, 3.0);
series1.add(4.0, 5.0);
series1.add(5.0, 5.0);
series1.add(6.0, 7.0);
series1.add(7.0, 7.0);
series1.add(8.0, 8.0);

XYSeries series2 = new XYSeries("Second");
series2.add(1.0, 5.0);
series2.add(2.0, 7.0);
series2.add(3.0, 6.0);
series2.add(4.0, 8.0);
series2.add(5.0, 4.0);
series2.add(6.0, 4.0);
series2.add(7.0, 2.0);
series2.add(8.0, 1.0);

XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);

return dataset;

}

private static JFreeChart createChart(XYDataset dataset) {

JFreeChart chart = ChartFactory.createXYLineChart(
"Line Chart Demo 6", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);

XYPlot plot = (XYPlot) chart.getXYPlot();

plot.getRenderer().setSeriesStroke(
1,
new BasicStroke(
1.0f,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND,
1.0f,
new float[] {10.0f, 6.0f},
0.0f
)
);


XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
// renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
renderer.setDefaultToolTipGenerator(new StandardXYToolTipGenerator());

renderer.setSeriesPaint(0, Color.BLACK);
renderer.setSeriesPaint(1, Color.BLACK);

plot.setRenderer(renderer);

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

return chart;

}

public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}

public static void main(String[] args) {
LineChartDemo4 demo = new LineChartDemo4(
"JFreeChart: LineChartDemo6.java");
demo.pack();
UIUtils.centerFrameOnScreen(demo);
demo.setVisible(true);
}

}

gw1500se
Posts: 23
Joined: Tue Nov 21, 2017 3:36 pm
antibot: No, of course not.

Re: Line Dashed

Post by gw1500se » Mon Jun 14, 2021 2:24 pm

Were you able to get this problem resolved? I have a similar issue in that I cannot get a dashed line at all. It seems 'BasicStroke' does not work.

It seems like other than David, no one is participating on this forum.

sergio_barcelos
Posts: 5
Joined: Thu May 06, 2021 6:51 pm
antibot: No, of course not.

Re: Line Dashed

Post by sergio_barcelos » Fri Jun 18, 2021 3:18 pm

hello gw1500se,

So I resolved by omitting all the default orientations. In this case my project was without ChartUtil enabled...

Use the DEMOs only for reference, you have to modify it until you get what you need...

See you.

sergio_barcelos
Posts: 5
Joined: Thu May 06, 2021 6:51 pm
antibot: No, of course not.

Re: Line Dashed

Post by sergio_barcelos » Fri Jun 18, 2021 3:20 pm

Send me your private contact and I'll send you the code I made it work, ok?

Locked