- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个包含折线图的 Excel 文件。我创建了图表并用数据填充了它,但我无法选择我想要的设计。如何使用 Apache poi 更改我的线条颜色、粗细和设计?
现在是白图:
我想要的是黑色的那种:
这是我的代码输入是这样的
people={100,200,150,140,130,120,110,100,90,85,80,75,70,65,60,55,50,45,40,35}
blueEye={10,110,140,135,130,120,110,100,90,85,80,75,70,65,60,55,50,45,40,35}
lineChart(people,blueEye);
package GeneticPropagation;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.MarkerStyle;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFLineChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ApachePoiLineChart {
public static void lineChart(int[] people,int[] blueEye) throws FileNotFoundException, IOException {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
String sheetName = "EyeLineChart";
XSSFSheet sheet = wb.createSheet(sheetName);
Row row = sheet.createRow((short) 0);
Cell cell = row.createCell((short) 0);
cell.setCellValue("Year");
cell = row.createCell((short) 1);
cell.setCellValue("people");
cell = row.createCell((short) 2);
cell.setCellValue("blue eye");
for (int i = 0; i < people.length; i++) {
row = sheet.createRow((short) i+1);
cell = row.createCell((short) 0);
cell.setCellValue(i);
cell = row.createCell((short) 1);
cell.setCellValue(people[i]);
cell = row.createCell((short) 2);
cell.setCellValue(blueEye[i]);
}
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 3, 0, 16, 16);
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText("Population growth chart");
chart.setTitleOverlay(false);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("Century");
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("Population");
XDDFNumericalDataSource<Double> countries = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(1, 20, 0, 0));
XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(1, 20, 1, 1));
XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(1, 20, 2, 2));
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(countries, area);
series1.setTitle("all of Population", null);
series1.setSmooth(false);
series1.setMarkerStyle(MarkerStyle.STAR);
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(countries, population);
series2.setTitle("blue eye Population", null);
series2.setSmooth(true);
series2.setMarkerSize((short) 6);
series2.setMarkerStyle(MarkerStyle.SQUARE);
chart.plot(data);
// Write output to an excel file
String filename = "line-chart-top-seven-countries.xlsx";
try (FileOutputStream fileOut = new FileOutputStream(filename)) {
wb.write(fileOut);
}
}
}
}
最佳答案
我修改了我的代码,它运行得更好
我们可以定义颜色并可以使用系列更改所有内容。...
for example:
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(countries, population);
color = "#0032FF";
lineSeriesColor(series1, XDDFColor.from(hex2Rgb(color)));
series1.setTitle("blue eye Population", null);
series1.setSmooth(true);
series1.setMarkerStyle(MarkerStyle.NONE);
series1.setMarkerStyle(...);
series1.setSmooth(..);
series1.getShapeProperties();
series1.setMarkerSize(...);
series1.setShapeProperties(...);
series1.getErrorBars();
series1.hasErrorBars();
series1.isSmooth();
series1.setTitle(...);
series1.plot();
...
public static void lineChart(int[] people, int[] blueEye) throws FileNotFoundException, IOException {试试 (XSSFWorkbook wb = new XSSFWorkbook()) {
String sheetName = "EyeLineChart";
XSSFSheet sheet = wb.createSheet(sheetName);
Row row = sheet.createRow((short) 0);
Cell cell = row.createCell((short) 0);
cell.setCellValue("Year");
cell = row.createCell((short) 1);
cell.setCellValue("people");
cell = row.createCell((short) 2);
cell.setCellValue("blue eye");
for (int i = 0; i < people.length; i++) {
row = sheet.createRow((short) i + 1);
cell = row.createCell((short) 0);
cell.setCellValue(i * 2000 / people.length);
cell = row.createCell((short) 1);
cell.setCellValue(people[i]);
cell = row.createCell((short) 2);
cell.setCellValue(blueEye[i]);
}
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 3, 0, (people.length + 2000) / 100, 16);
XSSFChart chart = drawing.createChart(anchor);
chart.setTitleText("Population growth chart");
chart.setTitleOverlay(false);
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setTitle("Year");
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("Population");
XDDFNumericalDataSource<Double> countries = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(1, people.length, 0, 0));
XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(1, people.length, 1, 1));
XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
new CellRangeAddress(1, people.length, 2, 2));
XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(countries, area);
String color = "#DEDEDE";
lineSeriesColor(series1, XDDFColor.from(hex2Rgb(color)));
series1.setTitle("all of Population", null);
series1.setSmooth(true);
series1.setMarkerStyle(MarkerStyle.DIAMOND);
XDDFLineChartData.Series series2 = (XDDFLineChartData.Series) data.addSeries(countries, population);
color = "#0032FF";
lineSeriesColor(series2, XDDFColor.from(hex2Rgb(color)));
series2.setTitle("blue eye Population", null);
series2.setSmooth(true);
series2.setMarkerStyle(MarkerStyle.NONE);
chart.plot(data);
// Write output to an excel file
String filename = "chart.xlsx";
try (FileOutputStream fileOut = new FileOutputStream(filename)) {
wb.write(fileOut);
}
}
}
改变颜色应该使用:
private static void lineSeriesColor(XDDFChartData.Series series, XDDFColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(color);
XDDFLineProperties line = new XDDFLineProperties();
line.setFillProperties(fill);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setLineProperties(line);
series.setShapeProperties(properties);
}
并且有一个 hex
到 RGB
转换器以备不时之需:
private static byte[] hex2Rgb(String colorStr) {
int r = Integer.valueOf(colorStr.substring(1, 3), 16);
int g = Integer.valueOf(colorStr.substring(3, 5), 16);
int b = Integer.valueOf(colorStr.substring(5, 7), 16);
return new byte[]{(byte) r, (byte) g, (byte) b};
}
你给它 2 个数组,它给你一个图表
关于java - 使用 Apache poi 在 excel 中设计折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62376595/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
折线图是一种用于可视化数据变化趋势的图表,它可以用于表示任何数值随着时间或类别的变化。 折线图由折线段和折线交点组成,折线段表示数值随时间或类别的变化趋势,折线交点表示数据的转折点。 折
我是 d3 和 typescript 的新手。 我正在尝试使用 d3 v4 和 typescript 创建一个简单的折线图。 但是,我收到了一个 typescript 错误,如下图所示: 问题是什么?
我是 Qlikview 的新手,经过几次失败的尝试后,我不得不请求一些有关 Qlikview 中图表的指导。我想创建折线图,其中包含: 一维 - 一个月的时间段按天数分割 一个表达式 - 每天创建的任
我正在尝试使用 Firebase 实时数据库中的数据在 Android 中制作折线图。 这是数据库的结构: enter image description here 这是代码: public clas
我有一个 TSQL 查询,它提供了一些性能基线的一个月数据。我用折线图显示数据。现在我想在报告中添加更多参数,以提供从两个不同月份选择数据的选项,并将其显示在同一个折线图中以进行比较。我不知道如何开始
我有一个简单(但非常大)的数据集,其中包含从 4 月到 8 月在不同站点进行的计数。 在 4 月中旬和 7 月之间,没有零计数 - 但零线从最早到最晚的日期延伸。 以下是用于制作上述图表的部分数据(列
我正在创建一个折线图,我想在不改变线条长度的情况下增加线条的高度或厚度。 在增加宽度属性之前,它看起来像这样: 增加宽度属性后,它看起来像这样: 我只想增加 height,但是没有这样的属性,所以我尝
我想在折线图的顶部显示值。我看过this answer这很有帮助,但它改变了折线图节点。我想要的是相同的想法,但不在节点上显示值,而是在它们附近(可能在它们的右侧和上方)显示值,例如:
我正在尝试使用谷歌图表以折线图的形式显示mysql数据。我认为问题出在我尝试格式化谷歌图表数据的部分。我的代码有什么问题吗? $sth = mysql_query("SELECT * FROM rea
我有 JavaFX LineChart 和一些带有 XYChart.Series 对象的数据 XYChart.Series series = new XYChart.Series(); series.
给定: 理想图 - 描绘了我的机器应该具有的预期读数。实际图表 - 描述我的机器在该实例中的实际读数。 X轴:来自机器的力(N) Y 轴:时间 这两个图都是使用 python 中的 pyplot 库创
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
<% function table2(total,table_x,table_y,all_width,all_height,line_n
我想根据数据中的一列离散值过滤使用 plotly 创建的图表。最终目标是能够使用按钮来更新过滤值,所以我不想事先过滤数据。 library(plotly) df % filter(group1=
我正在尝试在 JavaFX 中创建折线图。此折线图应该有一个轴 (y) 与数字和另一个轴 (x) 与日期。日期范围应该由用户使用两个日期选择器来选择。现在这是我的问题:折线图只有类别和数字轴。有什么方
我正在使用 nivo 折线图,并希望将 x 轴用作时间线,最多一分钟。 不幸的是,我无法呈现该图表,因为它无法正确读取日期。例如,这是我的数据的一部分: { x: "2020-04-24T13:07:
我有一个用 gRaphael 创建的折线图。它有轴和刻度线,但我想要网格线。是否有内置的方法来实现这一点或可以帮助我的附加库? 最佳答案 gRaphael 没有添加网格线的内置方法,但绘制它们非常容易
我正在生成一份报告,该报告是根据查询字符串在网页的页面加载时生成的。我在电子表格中生成的单元格数据完全符合我的要求。现在我需要添加一个折线图。数据是动态的,行数会有所不同。 搜索后没有信息,这与在 .
我正在尝试使用 highcharts 中每 x 秒更新一次的折线图。理想情况下,我希望它使用一些特定数据进行初始化,并每 x 秒轮询一次 Web 服务,并进行相应更新。 目前,我只是尝试使用网络服务中
我是一名优秀的程序员,十分优秀!