Mvc4+easyui-based Web development Framework Experience Summary (4)--Using the chart control Highcharts

Source: Internet
Author: User

When we do a variety of applications, we may use to chart statistics, previously exposed to a number of different chart controls, inadvertently discovered the chart control Highcharts, its powerful features and rich interactive effect, unforgettable. This article mainly introduces the use of chart control highcharts in Web development, as well as the unification of Chinese and other operations, so that our program functions richer, more beautiful content.

1, Highcharts Basic Introduction

Highcharts is a very popular and beautiful interface of pure JavaScript Chart library. It mainly consists of two parts: Highcharts and Highstock. Highcharts can provide intuitive, interactive charts for your website or Web application. Currently supports lines, splines, area, areaspline, column charts, bar charts, pie charts and scatter chart types. Highstock can easily create stock or general timeline charts for you. It includes advanced navigation options, preset date ranges, date selectors, scrolling and pan, and more.

Highcharts Official website: http://www.highcharts.com/

Highcharts demo:http://www.highcharts.com/demo/

Highcharts support graph, pie chart, histogram, gauge chart, scatter chart and so on dozens of kinds of graphics, interface display is very rich, 3D effect is also very good-looking. List a few for reference.

Highcharts uses JavaScript frameworks such as jquery to handle basic JavaScript tasks. Therefore, you need to refer to these script files at the head of the page before using Highcharts. If you use jquery as the basic framework, then you need to refer to both jquery and hightcharts two files on the head of the page.

Because I am in the Web development Framework, the main use of the Mvc+easyui way, because the file contains the following is shown.

@* add Jquery,easyui and Easyui to the language pack's JS file *@<ScriptType= "Text/javascript"Src= "~/content/jqueryeasyui/jquery.min.js"></Script><ScriptType= "Text/javascript"Src= "~/content/jqueryeasyui/jquery.easyui.min.js"></script> Span style= "color: #0000ff;" ><script type= " Text/javascript " Src=" ~/content/jqueryeasyui/locale /easyui-lang-zh_cn.js "></script> @* chart js file application *@ < script src= "~/content /jquerytools/highcharts/js/highcharts.js "></ Script>           

But for a better display, we typically add an icon with a predefined style in it, and add the script for the export function as shown below.

@* Chart js file application *@<Scriptsrc></script > <script src= "~/content/jquerytools/highcharts/js/modules/exporting.js" ></script> script src= "~/ Content/jquerytools/highcharts/js/themes/grid.js "></ script>        

Of course, if we scatter plots, 3D graphics and other content, but also need to introduce some additional JS file

    <src= "~/content/jquerytools/highcharts/js/highcharts-more.js"></script< src= "~/content/jquerytools/highcharts/js/highcharts-3d.js"></script> 

2, the chart generation operation

As I said earlier, this chart control is mostly implemented using jquery and JavaScript, and we'll analyze the demo code for the next pie chart.

$(function() {$ (' #container ')). Highcharts ({chart: {plotbackgroundcolor:Null, Plotborderwidth:Null, Plotshadow:False}, Title: {text: ' Browser market shares at a specific website, 2014 '}, tooltip: {pointformat: ' {series.name}: <b>{point.percentage:.1f}%</b> '}, Plotoptions: {pie: {allowpointselect:True, cursor: ' pointer ', DataLabels: {enabled:True], {name: ' Chrome ' truetrue] ] }] });}); 

The above script is mainly based on the data inside the series property to generate pie chart, then we actually developed, the data is certainly not fixed, generally we are in the dynamic way to assign value.

As I tend to use jquery Ajax way, call backstage to get the data, and then bound. So in this case, how to manipulate the script, let's look at the analysis.

First, we first initialize a chart object inside the script function, and set the data in the series to null.

            var Chart1 =NewHighcharts.chart ({Chart: {renderto: "Container1 ", Plotbackgroundcolor:Null, Plotborderwidth:Null, Plotshadow:False,}, Title: {text: ' Composition of group Molecular Company personnel '}, tooltip: {pointformat: ' {series.name}: <b>{point.y}</b> '}, Plotoptions: {pie: {allowpointselect: true, cursor: ' pointer ', DataLabels: {enabled: true, Format: ' <b>{point.name}</b>: {point.percentage:.1f}% ', style: {color: (highcharts.theme && Highcharts.theme.contrastTextColor) | | ' Black '}, ///Showinlegend:true }}, series: [{type: ' Pie ', Name: ' Number of personnel ', data: []}} );

The second step is to call the background connection through Ajax to get the data, and then bind to the specific properties on it, the specific code is as follows.

            get graph 1 data by Ajax            false;            var data1 = []; $.getjson ("function forif (Dict.hasownproperty (Key)} {Data1.push ([ Key, Dict[key]]); } }; Chart1.series[0].setdata (data1);});          

The HTML code of the chart is as follows, the main thing is to add a div,id to Container1, to place the chart.

                             <DivClass= "box"><DivClass= "Box-title"><DivStyle= "Float:left"><ImgSrc= "~/content/jqueryeasyui/themes/icons/customed/user.png"Alt=""Width= "20"Height= "20"/>Chart 1</Div><DivStyle= "Float:right; padding-right:10px; "> More</Div></Div><div class= " Box-content " Style=" height:310px "> <div id= "Container1"  style = "height:300px;max-width:500px" ></div> </div< Span style= "color: #0000ff;" >> </div>  

Complete the above code, we can run to see the following graphics, so it seems to be more cool.

3, the export function of the chart and the menu of Chinese

See from the above chart, each chart in the upper right corner, there is a menu function, there are some functions, such as printing pictures, export pictures and other operations, the specific menu performance as shown below.

But the above menu style I passed the Chinese processing, the default display effect is in English, as shown below.

Obviously the English menu, is not what we want, we need to be a better Chinese, then how to the General menu on the above, need each module is repeated the same Chinese, of course, no need. We can put it in the global settings.

Before we introduced, in order to make the chart show a better effect, we included a grid.js chart style, in fact there are other styles can be used, but I think the grid.js style is the best, as shown below.

Now that we have used this style setting, we can put some of the global settings, such as the Chinese operation, also here.

We at the bottom of this file, add a setoption operation code can, these Chinese menu, because I use the latest version, some parameters are inconsistent with the old version, so directed at this hard, should recommend encouragement under OH. Oh

Set the code for the Chinese. as shown below.

Apply the themevar highchartsoptions = highcharts.setoptions (highcharts.theme);  Chinese Chart Menu highcharts.setoptions ({lang: {contextbuttontitle: "Chart menu", Printchart: "Print picture", Downloadjpeg: "Download JPEG image", downloadpdf: "Download PDF document",downloadpng: "Download png image", downloadsvg: "Download svg vector", Exportbuttontitle: "Export Picture" });

Mvc4+easyui-based Web development Framework Experience Summary (4)--Using the chart control Highcharts

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.