public class createLineChartForSandSoil {
static JFreeChart chart;
public static XYSeries series;
public static void createLineChartForSandSoil(Document document) throws DocumentException, BadElementException, IOException {
Paragraph wordDegreeOfHeterogeneity = new Paragraph("Визначаємо ступінь неоднорідності піску:", smallFont);
document.add(wordDegreeOfHeterogeneity);
ChartPanel chartPanel = createChartPanel();
int width = 450;
int height = 350;
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesPaint(0, Color.BLACK);
plot.setRenderer(renderer);
plot.setOutlinePaint(Color.WHITE);
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.GRAY);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.GRAY);
File lineChart = new File("D:/LineChart.png");
ChartUtilities.saveChartAsPNG(lineChart, chart, width, height);
Image img = Image.getInstance("D:/LineChart.png");
img.scalePercent(60f);
document.add(img);
}
private static XYDataset createDataset() {
XYSeriesCollection dataset = new XYSeriesCollection();
series = new XYSeries("");
series.add(2.0, sumOfParticlesLess_ValueMoreThan2);
series.add(1.0, sumOfParticlesLess_Value1_2);
series.add(0.5, sumOfParticlesLess_Value05_1);
series.add(0.25, sumOfParticlesLess_Value025_05);
series.add(0.1, sumOfParticlesLess_Value01_025);
series.add(0.0, 0.0);
dataset.addSeries(series);
return dataset;
}
private static ChartPanel createChartPanel() {
String chartTitle = "";
String xAxisLabel = "Діаметр частинок d, мм";
String yAxisLabel = "Сума частинок, %";
XYDataset dataset = createDataset();
chart = ChartFactory.createXYLineChart(chartTitle, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, false, false, false);
return new ChartPanel(chart);
}
}
如何在Y = 60的点上获得X轴(hotrizontal)的值?方法.getAnnotationX()
和.getAnnotationY()
不起作用,不知道原因(cannot find method
)。有人可以帮帮我吗?
答案 0 :(得分:1)
如果60
是XYSeries
中某个点的纵坐标,则只需搜索List<XYDataItem>
返回的getItems()
并找到相应的横坐标即可。因为不是,您需要搜索包围点 - ( 0.25, 50)
和(0.5, 80)
。然后,您可以使用Regression.getOLSRegression()
方法查找连接两点的直线的斜率和截距。给定这些值,您可以求解相应的横坐标。或者,您可以重新排列线性方程的two-point form以找到所需的点。使用Regression.getOLSRegression()
的完整示例显示为here。