怎样用柱状图实现mysql数据库中的数据,现在我不知道怎么将数据库中的数据读取出来显示在柱状图上
关注:127 答案:3 mip版
解决时间 2021-01-24 11:35
提问者逍遥风
2021-01-23 18:56
请大家帮帮忙把 谢谢了
我是用jfreechart做的 就是将数据库中的数据取出来显示在柱状图上 应该怎么做 ?
最佳答案
二级知识专家自然卷的气质
2021-01-23 20:14
public class CategoryItemChart {
public static String generateBarChart(HttpSession session, PrintWriter pw,int w, int h,ArrayList list) {
String filename = null;
try {
CategoryDataset dataset = createDataset(list);
JFreeChart chart = ChartFactory.createBarChart(
"",//图表标题
"",//X轴标题
"",//Y轴标题
dataset,//数据集合
PlotOrientation.VERTICAL,//图表显示方向(水平、垂直)
true,//是否使用图例
true,//是否使用工具提示
false//是否为图表增加URL
);
chart.setBackgroundPaint(Color.white); // 1,设置整个图表背景颜色
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white); // 2,设置详细图表的显示细节部分的背景颜色
plot.setDomainGridlinePaint(Color.black); // 3,设置垂直网格线颜色
plot.setDomainGridlinesVisible(false); // 4,设置是否显示垂直网格线
plot.setRangeGridlinePaint(Color.yellow); // 5,设置水平网格线颜色
plot.setRangeGridlinesVisible(false); //6,设置是否显示水平网格线
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
BarRenderer render = (BarRenderer) plot.getRenderer();
// render.setItemMargin(0.0);
// render.setMinimumBarLength(0.0);
GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, new Color(255,200, 80), 0.0f, 0.0f, new Color(255, 255, 40));
GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, new Color(50,255, 50), 0.0f, 0.0f, new Color(100, 255, 100));
GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f,0.0f, new Color(255, 100, 100));
GradientPaint gp3 = new GradientPaint(0.0f, 0.0f, new Color(108,108, 255), 0.0f, 0.0f, new Color(150, 150, 200));
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setSeriesPaint(2, gp2);
renderer.setSeriesPaint(3, gp3);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.E / 6.0));
domainAxis.setLowerMargin(0.06);
domainAxis.setUpperMargin(0.06);
ValueAxis rAxis = plot.getRangeAxis();
rAxis.setUpperMargin(0.3);
rAxis.setLowerMargin(0.3);
render.setItemMargin(0.01);
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 14));
rAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
renderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));
renderer.setBaseItemLabelGenerator(new LabelGenerator(0.0));
renderer.setBaseItemLabelsVisible(true);
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session);
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
static class LabelGenerator implements CategoryItemLabelGenerator {
private double threshold;
public LabelGenerator(double threshold) {
this.threshold = threshold;
}
public String generateLabel(CategoryDataset dataset, int row, int column) {
String result = null;
final Number value = dataset.getValue(row, column);
if (value != null) {
final double v = value.doubleValue();
if (v > this.threshold) {
result = value.toString();
}
}
return result;
}
public String generateRowLabel(CategoryDataset dataset, int row) {
return null;
}
public String generateColumnLabel(CategoryDataset dataset, int column) {
return null;
}
}
private static CategoryDataset createDataset(ArrayList list) {
String s1 = "1";
String s2 = "2";
String c1 = "1";
String c2 = "2";
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(44, s1, c1);
dataset.setValue(48, s2, c2);
return dataset;
}
}
比较完整的一个得到柱图的代码,保存路径是临时文件,怎么从数据库取值应该会吧。把dataset处理一下就可以了。
全部回答
1楼臸釪樶初
2021-01-23 23:03
1.先用php算出数据在数据库中占的比例,
2.然后把这个值赋给柱状图的css样式属性(height),
3.给这个样式加一个背景颜色或者背景图(背景图的话别忘了设置背景图片定位方式)
2楼时光暗淡了承诺
2021-01-23 21:49
哪有这么麻烦..你如果是用的java直接使用jfreechart
我要举报
如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
点此我要举报以上信息!
推荐资讯
大家都在看