JFreeChart 或 Chart.js 库代码示例:从 Excel 文件读取数据并创建图表
使用 JFreeChart 库创建图表
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 文件并在浏览器中打开。
- 为了使用 XLSX 库解析 Excel 文件,你需要在 HTML 文件中添加以下代码:
<script src='https://cdn.jsdelivr.net/npm/xlsx'></script>
原文地址: https://www.cveoy.top/t/topic/fSYz 著作权归作者所有。请勿转载和采集!