highcharts实例和json数据显示

我是用mvc做的highcharts图表,其中遇到了一些问题,现在都解决了

这段代码是从后台Action里面返回的json字符串       #region 拼接字符串
            StringBuilder humstr = new StringBuilder();
            StringBuilder tempstr = new StringBuilder();
            StringBuilder surfstr = new StringBuilder();
            StringBuilder offsetstr = new StringBuilder();
            for (int i = 0; i < list3.Count; i++)
            {
                humstr.Append(list3[i].Humidity);
                humstr.Append(",");
                tempstr.Append(list3[i].Temperature);
                tempstr.Append(",");
                surfstr.Append(list3[i].SurfaceTemperature);
                surfstr.Append(",");
                offsetstr.Append(list3[i].ShapeChangeState);
                offsetstr.Append(",");
            }
            var TempData2 = humstr.ToString().TrimEnd(‘,‘);
            var TempData3 = tempstr.ToString().TrimEnd(‘,‘);
            var TempData4 = surfstr.ToString().TrimEnd(‘,‘);
            var TempData5 = offsetstr.ToString().TrimEnd(‘,‘);
            #endregion
            return Json(new { data2 = TempData2, data3 = TempData3, data4 = TempData4, data5 =TempData5 }, JsonRequestBehavior.AllowGet);

下面是前台highcharts图表,其中在series里面data显示的数组这里卡了一两天,原来是Y轴不能识别数据类型,当转为float在存入在一个数组里面就能识别了
        $(document).ready(function () {
            $.ajax({
                url: "@Url.Action("Xdata", "History")",
                type: "post",
                data: { rangeTimes: "", hiddenname: "" },
                dataType: "text",
                success: function (data) {
                    var ss = eval("(" + data + ")");
                    var array2 = ss.data2.split(‘,‘);
                    var array3 = ss.data3.split(‘,‘);
                    var array4 = ss.data4.split(‘,‘);
                    var array5 = ss.data5.split(‘,‘);
                    var humArray = [];
                    for (var i = 0; i < array2.length; i++) {
                        if (array2[i] == "null") {
                            humArray[i] = null;
                        } else {
                            humArray[i] = parseFloat(array2[i]);
                        }
                    }
                    var tempArray = [];
                    for (var i = 0; i < array3.length; i++) {
                        if (array3[i] == "null") {
                            tempArray[i] = null;
                        } else {
                            tempArray[i] = parseFloat(array3[i]);
                        }
                    }
                    var surfArray = [];
                    for (var i = 0; i < array4.length; i++) {
                        if (array4[i] == "null") {
                            surfArray[i] = null;
                        } else {
                            surfArray[i] = parseFloat(array4[i]);
                        }
                    }
                    var offsetArray = [];
                    for (var i = 0; i < array5.length; i++) {
                        if (array5[i] == "null") {
                            offsetArray[i] = null;
                        } else {
                            offsetArray[i] = parseFloat(array5[i]);
                        }
                    }
                    var rangeTimes = $("#rangeTimes").val();
                    //格式06/02/2015 - 06/03/2015
                    var strings = rangeTimes.trim(‘ ‘).split(‘-‘);
                    var now = strings[0];
                    var years = new Date().getFullYear(), months = new Date().getMonth(), days = new Date().getDate();
                    if (now != false) {
                        var timestrings = now.split(‘/‘);
                        years = timestrings[2];
                        months = timestrings[0] - 1;
                        days = timestrings[1];
                    }
                    //绘图
                    $(‘#container‘).highcharts({
                        chart: {
                            zoomType: ‘x‘,
                            spacingRight: 20
                        },
                        title: {
                            text: ‘电容监测系统图形化报表‘
                        },
                        credits: {
                            enabled: false
                        },
                        exporting: {
                            enabled: false
                        },
                        xAxis: {
                            type: ‘datetime‘,
                            title: {
                                text: null
                            }
                        },
                        yAxis: [
                            {// Primary yAxis
                                title: {
                                    text: ‘‘
                                }

                            }, { // Secondary yAxis
                                title: {
                                    text: ‘相角偏移量‘,
                                    style: {
                                        color: ‘#F7A35C‘
                                    }
                                },
                                labels: {
                                    format: ‘{value} °‘,
                                    style: {
                                        color: ‘#F7A35C‘
                                    }
                                },
                                opposite: true
                            }
                        ],
                        tooltip: {
                            shared: true
                        },
                        legend: {
                            //layout: ‘vertical‘,
                            align: ‘left‘,
                            x: 80,
                            verticalAlign: ‘top‘,
                            floating: true,
                            backgroundColor: ‘#FFFFFF‘
                        },
                        plotOptions: {
                            area: {
                                fillColor: {
                                    linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
                                    stops: [
                                        [0, Highcharts.getOptions().colors[0]],
                                        [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get(‘rgba‘)]
                                    ]
                                },
                                lineWidth: 1,
                                marker: {
                                    enabled: false
                                },
                                shadow: false,
                                states: {
                                    hover: {
                                        lineWidth: 1
                                    }
                                },
                                threshold: null
                            }
                        },
                        series: [
                        {
                            name: ‘相角偏移量‘,
                            type: ‘spline‘,
                            color: ‘#F7A35C‘,
                            yAxis: 1,
                            pointInterval: 3600 * 1000,
                            pointStart: Date.UTC(years, months, days),
                            data: offsetArray,
                            tooltip: {
                                valueSuffix: ‘ °‘
                            }
                        },
                        {
                            name: ‘湿度‘,
                            type: ‘spline‘,
                            color: ‘#4572A7‘,
                            pointInterval: 3600 * 1000,//24*3600*1000 one day   3600*1000 one hour
                            pointStart: Date.UTC(years, months, days),
                            data: humArray,
                            tooltip: {
                                valueSuffix: ‘ %‘
                            }
                        },
                        {
                            name: ‘表面温度‘,
                            type: ‘spline‘,
                            color: ‘#AA4643‘,
                            pointInterval: 3600 * 1000,
                            pointStart: Date.UTC(years, months, days),
                            data: tempArray,
                            tooltip: {
                                valueSuffix: ‘ ℃‘
                            }
                        },
                        {
                            name: ‘温度‘,
                            type: ‘spline‘,
                            color: ‘#89A54E‘,
                            pointInterval: 3600 * 1000,
                            pointStart: Date.UTC(years, months, days),
                            data: surfArray,
                            tooltip: {
                                valueSuffix: ‘ ℃‘
                            }
                        }]
                    });
                }
            });
        });
				
时间: 2024-11-07 17:13:31

highcharts实例和json数据显示的相关文章

highcharts实例教程一:结合php与mysql生成折线图

Highcharts是一款纯javascript和html5编写的图表库,不仅几乎能兼容所有pc浏览器,而且对ios和android手机端的兼容 性也不错,它能够很简单便捷的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.折线图.面积图.柱状图.饼图.散点图 等多达28种不同类型的图表,还支持3D立体图表的生成,可以满足你对Web图表的任何需求 !而且Highcharts对学习者.非商业机构是免费使用的. 案例场景:要求针对技术cto网站,直观地显示一周网站pv.

highcharts实例教程二:结合php与mysql生成饼图

上回我们分析了用highcharts结合php和mysql生成折线图的实例,这次我们以技术cto网站搜索引擎流量为例利用highcharts生成饼图. 饼图通常用在我们需要直观地显示各个部分所占的比例的时候,比如我们需要统计各大搜索引擎来的流量比例. 第一步:创建数据库保存各搜索引擎流量的pv数 CREATE TABLE `pie` (   `id` int(10) NOT NULL AUTO_INCREMENT,   `title` varchar(30) NOT NULL,   `pv` i

Highcharts axja 获取json对象动态生成报表生成

最近做个项目,项目经理想做一个统计报表,在网上查看些资料就选用Highchars 这里和大家分享下使用心得. 重点说明此代码是针对一个报表显示多个项对比显示. 直接贴代码:web端 <script type="text/javascript" src="js/jquery/jquery-1.7.2.js"></script>      <script type="text/javascript" src="

Highcharts实例

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>Highcharts Example</title> <script type="text/javascript" src="<%=request.getContext

dataTable的使用,接受json数据显示在页面上

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><h

FullCalendar应用——读取JSON数据

开发者可以使用FullCalendar创建强大的日历日程应用,FullCalendar提供了丰富的选项设置和方法调用,使得开发者可以轻松的创建各种功能的日历程序.本文将结合实例使用PHP读取MySQl数据,显示在FullCalendar日历中. 查看演示DEMO 根据FullCalendar日历插件说明文档中的介绍,日历主体事件数据的来源有三,一是直接以javascript数组的形式显示日历事件,二是获取JSON数据形式显示日历事件,三是函数回调的形式显示日历数据,三种调用数据的方式各有所用之处

比Newtonsoft.Json轻量快速简洁的实体JSON转换库YeaJur.Mapper

在使用MVC的时候,我们经常用到Newtonsoft.Json来进行实体和JSON 之间的转换,但是有时候,有些实体Newtonsoft.Json转换会出现异常.YeaJur.Mapper正是为了解决这些问题而来,并比Newtonsoft.Json轻量,转换速度快,使用简洁,测试结果如下 PK项 YeaJur.Mapper Newtonsoft.Json 版本 1.0 9.0.1 大小 6KB 514KB 实例(json格式) [ { "Products": [ { "Id&

使用Ajax与服务器(JSON)通信

Ajax这个词,不代表任何东西,它仅仅是称呼一系列促进客户端与服务器通信的技术时所用的一个术语.服务器通信时Ajax技术的核心内容,其目标就是从客户端向服务器发送信息,并接受后者的回传,以求在此过程中创建出更好地打用户体验来.Ajax之前所有的服务器通信都是在服务器上完成的,所以那是若想重绘页面的一部分,要么使用iframe(已淘汰),要么刷新整个页面.这两种方式都称不上是良好的用户体验. Ajax提供了两类服务器通信手段:同步通信和异步通信. 异步通信Ajax比同步通信要常见的多了,大概是98

使用json存储结构化数据

从文件中读写字符串很容易.数值就要多费点儿周折,因为read ()方法只会返回字符串,应将其传入int()这样的函数,就可以将'123'这样的字符串转换为对应的数值 123.当你想要保存更为复杂的数据类型,例如嵌套的列表和字典,手工解析和序列化它们将变得更复杂. 好在用户不是非得自己编写和调试保存复杂数据类型的代码,Python 允许你使用常用的数据交换格式JSON(JavaScript Object Notation).标准模块json可以接受 Python 数据结构,并将它们转换为字符串表示