JFreeChart - change SeriesStroke of chart lines from solid to dashed in one line(JFreeChart - 将图表线的 SeriesStroke 从实线更改为虚线)
问题描述
这里接受的答案(JFreechart(Java) - 如何绘制部分虚线和部分实线的线条?) 帮助我开始改变图表上的系列笔划线.在单步执行我的代码并观察更改后,我看到我的系列笔画实际上在它应该更改为dashedStroke"时(在某个日期dashedAfter"之后),但是当图表呈现时,整个系列线是虚线.如何让系列线一开始是实线,在设定的日期之后是虚线?
The answer accepted here (JFreechart(Java) - How to draw lines that is partially dashed lines and partially solid lines?) helped me start down the path of changing my seriesstroke lines on my chart. After stepping through my code and watching the changes, I see that my seriesstroke does in fact change to "dashedStroke" when it is supposed to (after a certain date "dashedAfter"), but when the chart is rendered the entire series line is dashed. How can I get a series line to be drawn solid at first and dashed after a set date?
/* series line modifications */
final Number dashedAfter = timeNowDate.getTime();
XYLineAndShapeRenderer render = new XYLineAndShapeRenderer() {
Stroke regularStroke = new BasicStroke();
Stroke dashedStroke = new BasicStroke(
1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {10.0f, 6.0f}, 0.0f );
@Override
public Stroke getItemStroke(int row, int column) {
Number xVal = cd.getXValue(row, column);
if (xVal.doubleValue() > dashedAfter.doubleValue()) {
return dashedStroke;
} else {
return regularStroke;
}
}
};
render.setBaseShapesVisible(false);
render.setBaseShapesFilled(true);
render.setDrawSeriesLineAsPath(true);
plot.setRenderer(render);
推荐答案
你试过实现AbstractRenderer#getItemStroke
吗?
在这个例子中,我对系列 2 使用 x > 4 的虚线:
In this example I'm using a dashed line for x > 4 for series 2:
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(){
Stroke soild = new BasicStroke(2.0f);
Stroke dashed = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
@Override
public Stroke getItemStroke(int row, int column) {
if (row == 2){
double x = dataset.getXValue(row, column);
if ( x > 4){
return dashed;
} else {
return soild;
}
} else
return super.getItemStroke(row, column);
}
};
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setBaseStroke(new BasicStroke(3));
plot.setRenderer(renderer);
虽然此示例使用的是 XYSeries
而不是您应该能够根据需要对其进行修改的日期.
Although this example is using and XYSeries
and not dates you shold be able to modify it for you needs.
这是完整的例子
import java.awt.BasicStroke;
import java.awt.Stroke;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class LineChartDemo2 extends ApplicationFrame {
public LineChartDemo2(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private static JFreeChart createChart(final XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"Line Chart Demo: XYLineAndShapeRenderer",
"X",
"Y",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false
);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(){
Stroke soild = new BasicStroke(2.0f);
Stroke dashed = new BasicStroke(1.0f,BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, new float[] {10.0f}, 0.0f);
@Override
public Stroke getItemStroke(int row, int column) {
if (row == 2){
double x = dataset.getXValue(row, column);
if ( x > 4){
return dashed;
} else {
return soild;
}
} else
return super.getItemStroke(row, column);
}
};
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setBaseStroke(new BasicStroke(3));
plot.setRenderer(renderer);
return chart;
}
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
return panel;
}
public static void main(String[] args) {
LineChartDemo2 demo = new LineChartDemo2(
"JFreeChart");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
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);
XYSeries series3 = new XYSeries("Third");
series3.add(3.0, 4.0);
series3.add(4.0, 3.0);
series3.add(5.0, 2.0);
series3.add(6.0, 3.0);
series3.add(7.0, 6.0);
series3.add(8.0, 3.0);
series3.add(9.0, 4.0);
series3.add(10.0, 3.0);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);
dataset.addSeries(series3);
return dataset;
}
}
这篇关于JFreeChart - 将图表线的 SeriesStroke 从实线更改为虚线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JFreeChart - 将图表线的 SeriesStroke 从实线更改为虚
基础教程推荐
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01