Java后台执行echarts画图

10 篇文章 0 订阅
8 篇文章 0 订阅

代码示例(参考网上别人的代码,自己整理的)

package com.pdf.pdfdemo;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.UUID;

public class EchartsTest {

    private static final String JSpath = "E:\\echarts\\echarts-convert.js";

    public static void main(String[] args) {

       //String options = "{\"title\":{\"text\":\"销售图\",\"subtext\":\"销售统计\",\"x\":\"CENTER\"},\"toolbox\": {\"feature\": {\"saveAsImage\": {\"show\": true,}}},\"tooltip\": {\"show\": true},\"legend\": {\"data\":[\"直接访问\",\"邮件营销\",\"联盟广告\",\"视频广告\",\"搜索引擎\"]}, \"series\":[{\"name\":\"访问来源\",\"type\":\"pie\",\"radius\": '55%',\"center\": ['50%', '60%'],\"data\":[{\"value\":335, \"name\":\"直接访问\"},{\"value\":310, \"name\":\"邮件营销\"},{\"value\":234, \"name\":\"联盟广告\"},{\"value\":135, \"name\":\"视频广告\"},{\"value\":1548, \"name\":\"搜索引擎\"}]}]}";
        
       String options=" {\n" +
               "    title: {\n" +
               "        text: '基础雷达图'\n" +
               "    },\n" +
               "    tooltip: {},\n" +
               "    legend: {\n" +
               "        data: ['预算分配(Allocated Budget)', '实际开销(Actual Spending)']\n" +
               "    },\n" +
               "    radar: {\n" +
               "        // shape: 'circle',\n" +
               "        name: {\n" +
               "            textStyle: {\n" +
               "                color: '#fff',\n" +
               "                backgroundColor: '#999',\n" +
               "                borderRadius: 3,\n" +
               "                padding: [3, 5]\n" +
               "            }\n" +
               "        },\n" +
               "        indicator: [\n" +
               "            { name: '销售(sales)', max: 6500},\n" +
               "            { name: '管理(Administration)', max: 16000},\n" +
               "            { name: '信息技术(Information Techology)', max: 30000},\n" +
               "            { name: '客服(Customer Support)', max: 38000},\n" +
               "            { name: '研发(Development)', max: 52000},\n" +
               "            { name: '市场(Marketing)', max: 25000}\n" +
               "        ]\n" +
               "    },\n" +
               "    series: [{\n" +
               "        name: '预算 vs 开销(Budget vs spending)',\n" +
               "        type: 'radar',\n" +
               "        // areaStyle: {normal: {}},\n" +
               "        data: [\n" +
               "            {\n" +
               "                value: [4300, 10000, 28000, 35000, 50000, 19000],\n" +
               "                name: '预算分配(Allocated Budget)'\n" +
               "            },\n" +
               "            {\n" +
               "                value: [5000, 14000, 28000, 31000, 42000, 21000],\n" +
               "                name: '实际开销(Actual Spending)'\n" +
               "            }\n" +
               "        ]\n" +
               "    }]\n" +
               "}";



       System.out.println(options.trim());

       String picPath = generateEChart(options);

    }

    /*
     * 主程序
     */
    public static  String generateEChart(String options) {
        String dataPath = writeFile(options);
        String fileName= UUID.randomUUID().toString() + ".png";
        String path = "D:/temp/Echart/" +fileName;
        try {
            File file = new File(path);     //文件路径
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            String cmd = "phantomjs " + JSpath + " -infile " + dataPath + " -outfile " + path;//生成命令行
            Process process = Runtime.getRuntime().exec(cmd);
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
            }
            input.close();

        } catch (IOException e) {
            e.printStackTrace();
        }finally{

        }
        return path;
    }

    /*
     *
     * options生成文件存储
     */
    public static String writeFile(String options) {
        String dataPath="D:\\chartData\\data"+ UUID.randomUUID().toString().substring(0, 8) +".json";
        try {
            /* option写入文本文件 用于执行命令*/
            File writename = new File(dataPath);
            if (!writename.exists()) {
                File dir = new File(writename.getParent());
                dir.mkdirs();
                writename.createNewFile(); //
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));
            out.write(options);
            out.flush(); // 把缓存区内容压入文件
            out.close(); // 最后关闭文件
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataPath;
    }
}

需要下载 phantomjs-2.1.1-windows

从百度 https://www.echartsjs.com/examples/zh/editor.html?c=radar
官网靠出ercharts 图 options 对象

echarts-converts.js为画图核心js,里面引用了min.js和jquery
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值