asp.net example of using echarts to show chart data

Source: Internet
Author: User
Tags arrays json require tostring

HTML code

The code is as follows Copy Code

<%@ Page language= "C #" autoeventwireup= true "codebehind=" EchartDemo.aspx.cs "inherits=" at. Web.Demo.EchartDemo "%>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
Example of <title>echart diagram </title>
<script src= ". /themes/scripts/jquery-1.8.2.min.js "></script>
<link href= ". /themes/styles/site.css "rel=" stylesheet "type=" Text/css "/>"
<script src= ". /themes/scripts/functionjs.js "type=" Text/javascript "></script>
<script type= "Text/javascript" >
Class
$ (function () {
$ (' #table2 '). Hide ();
})
Click on the Switch panel
var isfixedtableload = 1;
function Panel (obj) {
if (obj = = 1) {
$ (' #table1 '). Show ();
$ (' #table2 '). Hide ();
else if (obj = = 2) {
$ (' #table1 '). Hide ();
$ ("#table2"). Show ();
}
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div class= "Frmtop" >
<table style= "padding:0px; margin:0px; height:100% "cellpadding=" 0 "cellspacing=" 0 ">
<tr>
&LT;TD id= "Menutab" style= "Vertical-align:bottom;" >
<div id= "tab0" class= "Tabsel" onclick= "Gettabclick (This);p anel (1)" >
Energy consumption dynamic
</div>
<div id= "Tab1" class= "Tabremovesel" onclick= "Gettabclick" (This);p anel (2); >
Fill in dynamic
</div>
</td>
</tr>
</table>
</div>
<div class= "div-frm" style= "height:275px;" >
Dynamic--%> of <%--energy consumption
<div id= "Table1" >
<div id= "Divenergy" style= "width:610px; height:220px; " ></div>
</div>
<%--Fill Dynamic--%>
<div id= "Table2" >
<div id= "Divreport" style= "width:750px; height:250px; " ></div>
</div>
</div>
</form>
</body>
<script src= ". /themes/scripts/echarts/echarts.js "></script>
<script src= ". /themes/scripts/echarts/theme/macarons.js "></script>
<script type= "Text/javascript" >
var cityclick = ';
function Getunitenergy (unitname) {
Getcityenergy (Unitname, 2);
}
Require.config ({
Paths: {
Echarts: '.. /themes/scripts/echarts '
}
});
Require
[
' Echarts ',
' Echarts/chart/bar ',
' Echarts/chart/line ',
' Echarts/chart/pie ',
' Echarts/chart/funnel '
],
Drawechart//Asynchronously loaded callback function drawing chart
);
var Myenergychart;
var Myreportchart;
Create a Echarts chart method
function Drawechart (EC) {
---folded column---
Myenergychart = Ec.init (document.getElementById (' Divenergy '), macarons);
Define Chart Options
var options = {
Title: {
Text: "",
Subtext: ""
},
/*color: [' #0099FF ', ' #00CC00 ', ' #008080 ', ' #CC6600 ', ' #CC00CC ', ' #0033CC ', ' #003300 ', ' #FF0000 '],*/
ToolTip: {
Trigger: ' axis '
},
Legend: {
Data: ["]
},
Toolbox: {
Show:true,
Feature: {
Mark: {Show:false},
DataView: {show:false, readonly:false},
Magictype: {show:true, type: [' line ', ' Bar ']},
Restore: {show:true},
Saveasimage: {Show:false}
}
},
Grid: {
X:60,
Y:30,
X2:5,
Y2:30
},
Calculable:true,
Xaxis: [
{
Type: ' Category ',
Data: ["]
}
],
YAxis: [
{
Type: ' Value ',
Axislabel: {
Fomatter: "."
},
Splitarea: {show:true}
}
],
Series: [{
Name: "",
Type: "Line",
Data: [0]
}]
};
Select an empty chart
Myenergychart.setoption (options);
Myreportchart = Ec.init (document.getElementById (' Divreport '), macarons);
Select an empty chart
Myreportchart.setoption (options);
Default Load Fill
Getreport ("1");
Default Load Provincial
Getunitenergy (' 34 ');
}
Click on the button to get the chart data using Ajax method
function Getcityenergy (Citycode, level) {
Get the Options object for a chart
var options = Myenergychart.getoption ();
Get data through Ajax
$.ajax ({
Type: "Post",
Async:false,//Synchronous execution
URL: "Getchartdata.aspx?type=energydata",
DataType: "JSON",//return data in the form of JSON
Success:function (Result) {
if (result) {
Assigns the returned category and series objects to the category and series within the options object
Because Xaxis is an array here needs to be a form of xaxis[i]
Options.xaxis[0].data = result.category;
Options.series = result.series;
Options.legend.data = Result.legend;
Myenergychart.setoption (options);
Myenergychart.refresh ();
}
},
Error:function (errormsg) {
Alert ("Chart data loading failed!") ");
}
});
}
Click on the button to get the chart data using Ajax method
function Getreport (code) {
Get the Options object for a chart
var options = Myreportchart.getoption ();
Get data through Ajax
$.ajax ({
Type: "Post",
Async:false,//Synchronous execution
URL: "Getchartdata.aspx?type=reportdata",
DataType: "JSON",//return data in the form of JSON
Success:function (Result) {
if (result) {
Assigns the returned category and series objects to the category and series within the options object
Because Xaxis is an array here needs to be a form of xaxis[i]
Myreportchart.clear ();
Options.xaxis[0].data = result.category;
Options.series = result.series;
Options.legend.data = Result.legend;
Myreportchart.setoption (options);
}
},
Error:function (errormsg) {
Alert ("Chart data loading failed!") ");
}
});
}
Initial Load Chart data
$ (function () {
Getcityenergy ("", "");
Getunitenergy ("");
});
</script>


asp.net code:

The code is as follows Copy Code

<%@ Page language= "C #" autoeventwireup= true "codebehind=" GetChartData.aspx.cs "inherits=" at. Web.Demo.GetChartData "%>

asp.net Background Organization Data code:

The code is as follows Copy Code

Using Newtonsoft.json;
Using System;
Using System.Collections.Generic;
Using System.Data;
Using System.Data.SqlClient;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace at. Web.demo
{
public partial class GetChartData:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
String type = request["type"];
if (!string. IsNullOrEmpty (Type))
{
Switch (type)
{
Case "Energydata":
Getenergydataajaxdata ("", "");
Break
Case "ReportData":
Getreportdataajaxdata ("");
Break
}
}
}
<summary>
Get data and return in JSON format data
</summary>
private void Getenergydataajaxdata (String level, string code)
{
Considering that the category of the chart is a string array, this defines a string list
list<string> categorylist = new list<string> ();
Considering that the series data for a chart is an array of objects, here's an extra definition of a series class
list<series> serieslist = new list<series> ();
Given that the Echarts chart needs to set the legend data array to the series name collection, you need to define a legend array
list<string> legendlist = new list<string> ();
DataTable dt = null;
using (SqlConnection con = new SqlConnection ("Data source=.; User Id=sa; password=111111;database=at_db; "))
{
String strSQL = "SELECT top * from T_echartdata1";
Using (SqlDataAdapter adapter = new SqlDataAdapter (strSQL, con))
{
DataSet ds = new DataSet ();
Adapter. Fill (DS);
DT = ds. Tables[0];
}
}
HTM = htm& (dt. rows.count+ "Piece of data! ");
Set legend Array
Legendlist.add ("Energy consumption this year"); The names here must be consistent with the name of each group of series series
Legendlist.add ("Last year energy consumption"); The names here must be consistent with the name of each group of series series
Series thisseriesobj = new Series ();
thisseriesobj.id = 0;
Thisseriesobj.name = "Energy consumption this year";
Thisseriesobj.type = "line"; Linear graph rendering
Thisseriesobj.data = new list<object> ();
Thisseriesobj.itemstyle = new ItemStyle {normal = new normal {color = ' #0099FF '}};
Series lastseriesobj = new Series ();
Lastseriesobj.id = 1;
Lastseriesobj.name = "Last year energy consumption";
Lastseriesobj.type = "line"; Linear graph rendering
Lastseriesobj.data = new list<object> ();
Lastseriesobj.itemstyle = new ItemStyle {normal = new normal {color = ' #00CC00 '}};
foreach (DataRow dr in Dt. Rows)
{
Categorylist.add (Dr[0]. ToString () + "month");
THISSERIESOBJ.DATA.ADD (dr[1]);
LASTSERIESOBJ.DATA.ADD (dr[2]);
}
Pressing a Sereis object into the list of Sereis arrays
Serieslist.add (Thisseriesobj);
Serieslist.add (Lastseriesobj);
Finally call the correlation function to convert the list to JSON
Because we need to return category and series, legend multiple objects here ourselves in the new object to encapsulate these two objects
var newObj = new
{
Category = CategoryList,
Series = Serieslist,
Legend = Legendlist
};
Response returns the JSON data for the new object
htm = htm& (Jsonconvert.serializeobject (NEWOBJ, Formatting.none));
Response.End ();
}
<summary>
Get data and return in JSON format data
</summary>
private void Getreportdataajaxdata (string code)
{
Considering that the category of the chart is a string array, this defines a string list
list<string> categorylist = new list<string> ();
Considering that the series data for a chart is an array of objects, here's an extra definition of a series class
list<series> serieslist = new list<series> ();
Given that the Echarts chart needs to set the legend data array to the series name collection, you need to define a legend array
list<string> legendlist = new list<string> ();
DataTable dt = null;
using (SqlConnection con = new SqlConnection ("Data source=.; User Id=sa; password=111111;database=at_db; "))
{
String strSQL = "SELECT * from T_echartdata2";
Using (SqlDataAdapter adapter = new SqlDataAdapter (strSQL, con))
{
DataSet ds = new DataSet ();
Adapter. Fill (DS);
DT = ds. Tables[0];
}
}
HTM = htm& (dt. Rows.Count + "Piece of data! ");
Set legend Array
Legendlist.add ("Electricity"); The names here must be consistent with the name of each group of series series
Legendlist.add ("Water"); The names here must be consistent with the name of each group of series series
Legendlist.add ("Coal"); The names here must be consistent with the name of each group of series series
Legendlist.add ("natural gas"); The names here must be consistent with the name of each group of series series
Legendlist.add ("petrol"); The names here must be consistent with the name of each group of series series
Legendlist.add ("Diesel"); The names here must be consistent with the name of each group of series series
Legendlist.add ("Heat"); The names here must be consistent with the name of each group of series series
Legendlist.add ("other"); The names here must be consistent with the name of each group of series series
Series dianseries = new Series ();
dianseries.id = 0;
Dianseries.name = "Electricity";
Dianseries.type = "line"; Linear graph rendering
Dianseries.data = new list<object> ();
Dianseries.itemstyle = new ItemStyle {normal = new normal {color = ' #0099FF '}};
Series shuiseries = new Series ();
Shuiseries.id = 1;
Shuiseries.name = "Water";
Shuiseries.type = "line"; Linear graph rendering
Shuiseries.data = new list<object> ();
Shuiseries.itemstyle = new ItemStyle {normal = new normal {color = ' #00CC00 '}};
Series yuanmeiseries = new Series ();
Yuanmeiseries.id = 2;
Yuanmeiseries.name = "Coal burning";
Yuanmeiseries.type = "line"; Linear graph rendering
Yuanmeiseries.data = new list<object> ();
Yuanmeiseries.itemstyle = new ItemStyle {normal = new normal {color = ' #008080 '}};
Series tianranqiseries = new Series ();
Tianranqiseries.id = 3;
Tianranqiseries.name = "natural gas";
Tianranqiseries.type = "line"; Linear graph rendering
Tianranqiseries.data = new list<object> ();
Tianranqiseries.itemstyle = new ItemStyle {normal = new normal {color = ' #CC6600 '}};
Series qiyouseries = new Series ();
Qiyouseries.id = 4;
Qiyouseries.name = "petrol";
Qiyouseries.type = "line"; Linear graph rendering
Qiyouseries.data = new list<object> ();
Qiyouseries.itemstyle = new ItemStyle {normal = new normal {color = ' #CC00CC '}};
Series chaiyouseries = new Series ();
Chaiyouseries.id = 5;
Chaiyouseries.name = "Diesel";
Chaiyouseries.type = "line"; Linear graph rendering
Chaiyouseries.data = new list<object> ();
Chaiyouseries.itemstyle = new ItemStyle {normal = new normal {color = ' #0033CC '}};
Series reliseries = new Series ();
Reliseries.id = 6;
Reliseries.name = "Heat";
Reliseries.type = "line"; Linear graph rendering
Reliseries.data = new list<object> ();
Reliseries.itemstyle = new ItemStyle {normal = new normal {color = ' #003300 '}};
Series qitaseries = new Series ();
Qitaseries.id = 7;
Qitaseries.name = "other";
Qitaseries.type = "line"; Linear graph rendering
Qitaseries.data = new list<object> ();
Qitaseries.itemstyle = new ItemStyle {normal = new normal {color = ' #FF0000 '}};
foreach (DataRow dr in Dt. Rows)
{
Categorylist.add (String. Format ("{0} years {1} months", dr["Reportyear"], dr["Reportmonth"));
DIANSERIES.DATA.ADD (String. IsNullOrEmpty (dr["Dian"). ToString ())? 0:dr["Dian"]);
SHUISERIES.DATA.ADD (String. IsNullOrEmpty (dr["SHUI"). ToString ())? 0:dr["SHUI"]);
YUANMEISERIES.DATA.ADD (String. IsNullOrEmpty (dr["Yuanmei"). ToString ())? 0:dr["Yuanmei"]);
TIANRANQISERIES.DATA.ADD (String. IsNullOrEmpty (dr["Tianrq"). ToString ())? 0:dr["TIANRQ"]);
QIYOUSERIES.DATA.ADD (String. IsNullOrEmpty (dr["qiyou"). ToString ())? 0:dr["Qiyou"]);
CHAIYOUSERIES.DATA.ADD (String. IsNullOrEmpty (dr["chaiyou"). ToString ())? 0:dr["Chaiyou"]);
RELISERIES.DATA.ADD (String. IsNullOrEmpty (dr["Reli"). ToString ())? 0:dr["Reli"]);
QITASERIES.DATA.ADD (String. IsNullOrEmpty (dr["Qtny"). ToString ())? 0:dr["Qtny"]);
}
Pressing a Sereis object into the list of Sereis arrays
Serieslist.add (dianseries);
Serieslist.add (shuiseries);
Serieslist.add (yuanmeiseries);
Serieslist.add (tianranqiseries);
Serieslist.add (qiyouseries);
Serieslist.add (chaiyouseries);
Serieslist.add (reliseries);
Serieslist.add (qitaseries);
Finally call the correlation function to convert the list to JSON
Because we need to return category and series, legend multiple objects here ourselves in the new object to encapsulate these two objects
var newObj = new
{
Category = CategoryList,
Series = Serieslist,
Legend = Legendlist
};
Response returns the JSON data for the new object
htm = htm& (Jsonconvert.serializeobject (NEWOBJ, Formatting.none));
Response.End ();
}
}
<summary>
Define a series class to set some basic properties for each group of Sereis
</summary>
Class Series
{
<summary>
Sereis Sequence Group ID
</summary>
public int ID
{
Get
Set
}
<summary>
Series Sequence Group name
</summary>
public string Name
{
Get
Set
}
<summary>
Series Sequence group render chart type (line, column, bar, and so on)
</summary>
public String type
{
Get
Set
}
<summary>
ItemStyle of Series sequence Group
</summary>
public Object ItemStyle
{
Get
Set
}
<summary>
The data for the series sequence group is an array of data types
</summary>
Public list<object> Data
{
Get
Set
}
}
Class Normal
{
<summary>
Color
</summary>
public string Color
{
Get
Set
}
}
Class ItemStyle
{
<summary>
Normal
</summary>
public Object Normal
{
Get
Set
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.