java实现JFreeChart生成SVG图,柱状图坐标为0不显示问题,生成折线图

本文介绍了如何使用Java的JFreeChart库生成SVG图形,特别是针对柱状图中坐标为0时不显示的问题,以及如何生成折线图。通过示例代码展示了具体实现过程。
摘要由CSDN通过智能技术生成

java实现JFreeChart生成SVG图,柱状图坐标为0不显示问题,折线图
先看效果:
不显示坐标的柱状图在这里插入图片描述

直接上代码:
1、保存为svg图代码:

package com.example.demo.jfreechart;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.jfree.chart.JFreeChart;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class SaveChartSVG {
   
    public static boolean save(String path,JFreeChart chart,int x,int y,int width,int height){
   
        try {
   
            // THE FOLLOWING CODE BASED ON THE EXAMPLE IN THE BATIK DOCUMENTATION...
            // Get a DOMImplementation
            DOMImplementation domImpl = GenericDOMImplementation
                    .getDOMImplementation();
            // Create an instance of org.w3c.dom.Document
            Document document = domImpl.createDocument(null, "svg", null);
            // Create an instance of the SVG Generator
            SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
            svgGenerator.setSVGCanvasSize(new Dimension(width,height));
            // set the precision to avoid a null pointer exception in Batik 1.5
            svgGenerator.getGeneratorContext().setPrecision(6);
            // Ask the chart to render into the SVG Graphics2D implementation
            chart.draw(svgGenerator, new Rectangle2D.Double(x, y, width, height), null);
            // Finally, stream out SVG to a file using UTF-8 character to
            // byte encoding
            boolean useCSS = true;
            Writer out = new OutputStreamWriter(new FileOutputStream(new File(
                    path)), "UTF-8");
            svgGenerator.stream(out, useCSS);
            return true;
        }catch (Exception e){
   
            e.printStackTrace();
        }
        return false;
    }
}

生成柱状图:

package com.example.demo.jfreechart;

import com.example.demo.util.ObjectUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.*;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.GradientBarPainter;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import java.awt.*;
import java.text.DecimalFormat;

public class 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值