将其修改为JFreeCharts库或Chartjs库的代码在文件inputxlsx中有一个P1工作表用其中A2-A50的数据作为水平轴水平轴要为时间格式用B2-B50的数据作为垂直轴用C2-C50的数据作为次要垂直轴
对于JFreeCharts库的代码,可以使用以下代码来实现:
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.awt.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class JFreeChartsExample extends ApplicationFrame {
public JFreeChartsExample(String title) throws IOException, ParseException {
super(title);
JFreeChart chart = createChart();
ChartPanel panel = new ChartPanel(chart);
panel.setPreferredSize(new java.awt.Dimension(800, 600));
setContentPane(panel);
}
private JFreeChart createChart() throws IOException, ParseException {
XYDataset dataset1 = createDataset("input.xlsx", "P1");
DateAxis timeAxis = new DateAxis("Time");
timeAxis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
NumberAxis valueAxis1 = new NumberAxis("Value");
NumberAxis valueAxis2 = new NumberAxis("Secondary Value");
XYPlot plot = new XYPlot(dataset1, timeAxis, valueAxis1, null);
plot.setOrientation(PlotOrientation.VERTICAL);
XYDataset dataset2 = createDataset("input.xlsx", "P1");
plot.setDataset(1, dataset2);
plot.setRangeAxis(1, valueAxis2);
plot.mapDatasetToRangeAxis(1, 1);
JFreeChart chart = new JFreeChart("Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(Color.white);
return chart;
}
private XYDataset createDataset(String filename, String sheetName) throws IOException, ParseException {
TimeSeries series1 = new TimeSeries("Value");
TimeSeries series2 = new TimeSeries("Secondary Value");
InputStream inputStream = new FileInputStream(filename);
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheet(sheetName);
for (int i = 1; i <= 50; i++) {
Row row = sheet.getRow(i);
Cell dateCell = row.getCell(0);
Cell valueCell1 = row.getCell(1);
Cell valueCell2 = row.getCell(2);
Date date = dateCell.getDateCellValue();
double value1 = valueCell1.getNumericCellValue();
double value2 = valueCell2.getNumericCellValue();
Day day = new Day(date);
series1.add(day, value1);
series2.add(day, value2);
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);
return dataset;
}
public static void main(String[] args) {
try {
JFreeChartsExample example = new JFreeChartsExample("Chart Example");
example.pack();
RefineryUtilities.centerFrameOnScreen(example);
example.setVisible(true);
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
}
对于Chart.js库的代码,可以使用以下代码来实现:
<!DOCTYPE html>
<html>
<head>
<title>Chart.js Example</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="chart"></canvas>
<script>
const ctx = document.getElementById('chart').getContext('2d');
fetch('input.xlsx')
.then(response => response.arrayBuffer())
.then(data => {
const workbook = XLSX.read(data, {type: 'array'});
const sheet = workbook.Sheets['P1'];
const dates = XLSX.utils.sheet_to_json(sheet, {header: 1, range: 'A2:A50'});
const values1 = XLSX.utils.sheet_to_json(sheet, {header: 1, range: 'B2:B50'});
const values2 = XLSX.utils.sheet_to_json(sheet, {header: 1, range: 'C2:C50'});
const labels = dates.map(date => new Date(date));
const data1 = values1.map(value => parseFloat(value));
const data2 = values2.map(value => parseFloat(value));
new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'Value',
data: data1,
borderColor: 'blue',
fill: false
},
{
label: 'Secondary Value',
data: data2,
borderColor: 'red',
fill: false
}
]
},
options: {
scales: {
x: {
type: 'time',
time: {
unit: 'day',
displayFormats: {
day: 'yyyy-MM-dd'
}
},
ticks: {
source: 'labels'
}
},
y: {
beginAtZero: true
}
}
}
});
});
</script>
</body>
</html>
请注意,对于Chart.js的代码,需要引入Chart.js库,并且使用fetch函数来加载Excel文件。此外,Chart.js需要在浏览器中运行,因此需要将代码保存为一个HTML文件并在浏览器中打开。
原文地址: https://www.cveoy.top/t/topic/iikT 著作权归作者所有。请勿转载和采集!