使用AngularJS结合Highchart图表动态获取JSON格式数据

Highchart折线图代码
    $(function () {
        var chartLine = new Highcharts.Chart({
            chart: {
                type: 'line',
                <em><span style="color:#ff0000;">renderTo: 'container2'</span></em>
            },
            title: {
                text: '上月已还款金额与本月账单金额对比结果'
            },
            subtitle: {
                text: ''
            },
            xAxis: {
                categories: []
            },
            yAxis: {
                title: {
                    text: '单位 (元)'
                }
            },
            tooltip: {
                enabled: false,
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br>' + this.x + ': ' + this.y + '°C';
                }
            },
            plotOptions: {
                line: {
                    dataLabels: {
                        enabled: true
                    },
                    enableMouseTracking: false
                }
            },
            series: [{
                name: '上月还款金额',
                data: []
            }, {
                name: '本月消费金额',
                data: []
            }]
        });

        function getFormLine() {
            return $http.get("/billsDetail/twoMonthChartData").success(function (response) {
                //为图表设置值
                <strong><span style="color:#ff0000;">chartLine.series[0].setData(response.data.lastMonth);
                chartLine.series[1].setData(response.data.thisMonth);
                chartLine.xAxis[0].setCategories(eval(response.data.categorys));</span></strong>
            }).error(function (response) {
                $log.debug("请求超时或网络故障!获得列表失败!")
            });

        }

        getFormLine();
    });

后端拼JSON代码

   public JSONObject listLastMonthAndThismonthBillsData() {
        JSONObject json =new JSONObject();
        List<String> categorys =new ArrayList<String>();
        List<Double> lastMonthData=new ArrayList<Double>();
        List<Double> thisMonthData=new ArrayList<Double>();
        String sql="select lastmonth_repayment,thismonth_bill,period from credit_card_bills order by period asc " ;
        List<JSONObject> resultdata = billsDetailDao.list(sql);
        for(JSONObject dataItem:resultdata)
        {
            categorys.add(dataItem.getString("period"));
            lastMonthData.add(dataItem.getDouble("lastmonth_repayment"));
            thisMonthData.add(dataItem.getDouble("thismonth_bill"));

        }
        json.put("categorys",categorys);
        json.put("lastMonth",lastMonthData);
        json.put("thisMonth",thisMonthData);
        return json;
    }

HTML代码

    <div  id="container2">
    </div>
效果图



Highcharts是一个用于制作交互式图表的JavaScript库,可以创建各种类型的图表,包括复合图表。 要创建一个复合图表,需要使用Highcharts的组合图表功能,该功能允许将多个图表组合在一起以显示多个数据系列。以下是一个创建复合图表的基本步骤: 1. 创建一个空的Highcharts图表对象。 2. 添加一个或多个数据系列,每个系列对应一个不同的图表类型,例如柱状图、折线图、面积图等。 3. 配置每个数据系列的选项,包括颜色、标签、线型等。 4. 配置图表的整体选项,例如标题、图例、坐标轴等。 5. 将图表渲染到HTML页面中。 下面是一个创建包含柱状图和折线图的复合图表的示例代码: ```javascript // 创建一个空的Highcharts图表对象 var chart = Highcharts.chart('container', { title: { text: '复合图表示例' }, xAxis: { categories: ['一月', '二月', '三月', '四月', '五月', '六月'] }, yAxis: [{ // 配置左侧Y轴 title: { text: '销售额' } }, { // 配置右侧Y轴 title: { text: '利润' }, opposite: true }], series: [{ // 添加第一个数据系列,使用柱状图 name: '销售额', type: 'column', yAxis: 0, data: [100, 200, 150, 250, 300, 200] }, { // 添加第二个数据系列,使用折线图 name: '利润', type: 'line', yAxis: 1, data: [50, 100, 80, 120, 150, 100] }] }); ``` 在上面的代码中,我们创建了一个包含柱状图和折线图的复合图表,其中左侧Y轴表示销售额,右侧Y轴表示利润。柱状图表示销售额,折线图表示利润。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值