JAVA Echarts-legend不显示解决办法

【问题描述】:有些小伙伴发现legend没有显示,如图所示,那么这到底是什么情况呢?

【问题处理】:注意legend中的data属性要与series中的name属性保持一致即可

var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
app.title = '堆叠条形图';

option = {
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐标轴指示器,坐标轴触发有效
            type : 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
        }
    },
    legend: {
        data: ['直接访问', '邮件营销','联盟广告','视频广告','搜索引擎']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis:  {
        type: 'value'
    },
    yAxis: {
        type: 'category',
        data: ['周一','周二','周三','周四','周五','周六','周日']
    },
    series: [
        {
            name: '直接访问',
            type: 'bar',
            stack: '总量',
            label: {
                normal: {
                    show: true,
                    position: 'insideRight'
                }
            },
            data: [320, 302, 301, 334, 390, 330, 320]
        },
        {
            name: '邮件营销',
            type: 'bar',
            stack: '总量',
            label: {
                normal: {
                    show: true,
                    position: 'insideRight'
                }
            },
            data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
            name: '联盟广告',
            type: 'bar',
            stack: '总量',
            label: {
                normal: {
                    show: true,
                    position: 'insideRight'
                }
            },
            data: [220, 182, 191, 234, 290, 330, 310]
        },
        {
            name: '视频广告',
            type: 'bar',
            stack: '总量',
            label: {
                normal: {
                    show: true,
                    position: 'insideRight'
                }
            },
            data: [150, 212, 201, 154, 190, 330, 410]
        },
        {
            name: '搜索引擎',
            type: 'bar',
            stack: '总量',
            label: {
                normal: {
                    show: true,
                    position: 'insideRight'
                }
            },
            data: [820, 832, 901, 934, 1290, 1330, 1320]
        }
    ]
};

 

为了在Java后端实现ECharts折线图,您需要遵循以下步骤: 1.首先,您需要在Java项目中引入ECharts库。您可以从以下链接下载ECharts-2.2.7.jar文件:http://mvnrepository.com/artifact/com.github.abel533/ECharts 2.接下来,您需要在前端使用ECharts.js。您可以从以下链接下载ECharts.js文件:http://echarts.baidu.com/echarts2/ 3.在Java代码中,您需要使用ECharts库提供的类来创建图表。以下是一个简单的示例代码,用于创建一个折线图: ```java import com.github.abel533.echarts.Option; import com.github.abel533.echarts.axis.CategoryAxis; import com.github.abel533.echarts.axis.ValueAxis; import com.github.abel533.echarts.code.AxisType; import com.github.abel533.echarts.code.LineType; import com.github.abel533.echarts.series.Line; import com.github.abel533.echarts.series.Series; public class EChartsLineChart { public Option getLineChart() { // 创建Option对象 Option option = new Option(); // 设置图表标题 option.title().text("折线图"); // 设置图表工具栏 option.toolbox().show(true); // 设置图表图例 option.legend().data("数据1", "数据2"); // 设置X轴 CategoryAxis xAxis = new CategoryAxis(); xAxis.type(AxisType.category); xAxis.data("一月", "二月", "三月", "四月", "五月", "六月"); option.xAxis(xAxis); // 设置Y轴 ValueAxis yAxis = new ValueAxis(); yAxis.type(AxisType.value); option.yAxis(yAxis); // 添加数据系列 Line line1 = new Line(); line1.name("数据1"); line1.type(Series.Type.line); line1.data(10, 20, 30, 40, 50, 60); Line line2 = new Line(); line2.name("数据2"); line2.type(Series.Type.line); line2.data(20, 30, 40, 50, 60, 70); option.series(line1, line2); return option; } } ``` 4.在您的Java代码中,您需要将数据整合到Option对象中,并将其返回给前端。以下是一个简单的示例代码: ```java import com.github.abel533.echarts.Option; import com.github.abel533.echarts.json.GsonOption; public class EChartsController { public String getLineChartData() { // 创建ECharts折线图对象 EChartsLineChart lineChart = new EChartsLineChart(); // 获取Option对象 Option option = lineChart.getLineChart(); // 将Option对象转换为JSON字符串 GsonOption gsonOption = new GsonOption(); gsonOption.setOption(option); String json = gsonOption.toString(); // 返回JSON字符串 return json; } } ``` 5.最后,在前端代码中,您需要使用Ajax从后端获取数据,并使用ECharts.js将数据渲染为折线图。以下是一个简单的示例代码: ```javascript $.ajax({ url: "/getLineChartData", type: "GET", dataType: "json", success: function (data) { // 使用ECharts.js将数据渲染为折线图 var myChart = echarts.init(document.getElementById("lineChart")); myChart.setOption(data); } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值