简体   繁体   English

来自ASP.NET C#中数据库的highcharts值

[英]highcharts values from database in asp.net c#

Tell me how to get value from database into highchart , 告诉我如何从数据库中获取价值,

Code of high chart. 高图表代码。

$('#account_details_chart').highcharts({ chart: { type: 'areaspline' }, $('#account_details_chart')。highcharts({图表:{类型:'areaspline'},

            legend: {
                layout: 'inline-block',
                align: 'right',
                verticalAlign: 'top',
                x: 0,
                y: 10,
                floating: true,
                borderWidth: 1,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
            },
            xAxis: {
                categories: company,
                plotBands: [{ // visualize the weekend
                    from: 6.5,
                    to: 6.5,
                    color: 'rgba(68, 170, 213, .2)'
                }]
            },
            yAxis: {
                title: {
                    text: 'Amount In Rupees'
                }
            },
            tooltip: {
                shared: true,
                valuePrefix: 'Rs. ',
                valueSuffix: ' /-'
            },
            credits: {
                enabled: true
            },
            plotOptions: {
                areaspline: {
                    fillOpacity: 0.5
                }
            },
            series: [{
                name: 'Debit Amount',
                data: debit_amount
            }, {
                name: 'Credit Amount',
                data: credit_amount
            }]
        });
    });

code behind. 后面的代码。

protected void Page_Load(object sender, EventArgs e) { 受保护的无效Page_Load(对象发送者,EventArgs e){

    List<string> tempString = new List<string>();
    tempString.Add("Hello");
    tempString.Add("World");
    tempString.Add("Hello");
    tempString.Add("World");
    tempString.Add("Hello");
    tempString.Add("World");
    //string builder for binding data in script
    StringBuilder sb = new StringBuilder();
    sb.Append("<script>");
    sb.Append("var company = new Array;");
    foreach (string str in tempString)
    {
        sb.AppendFormat("company.push('{0}');", str);
    }
    sb.Append("</script>");
    //sending data through client script register
    ClientScript.RegisterStartupScript(this.GetType(), "TestArrayScript", sb.ToString());

    List<int> DebitAmount = new List<int>();
    DebitAmount.Add(2);
    DebitAmount.Add(3);
    DebitAmount.Add(2);
    DebitAmount.Add(4);

    StringBuilder sb2 = new StringBuilder();
    sb2.Append("<script type='text/javascript'>");
    sb2.Append("var debit_amount = new Array;");
    foreach (int str2 in DebitAmount)
    {
        sb2.AppendFormat("debit_amount.push('{0}');", str2);
    }
    sb2.Append("</script>");
    //sending data through client script register

    ClientScript.RegisterStartupScript(GetType(), "ArrayScript", sb2.ToString());

}

company array is working fine on the x-axis but the debit_amount array is not working 公司数组在x轴上工作正常,但是debit_amount数组不工作

Create internal service which will return an ajax with correct format which the HighCharts will use with your data from database. 创建内部服务,该服务将返回正确格式的ajax, HighCharts将使用该格式与数据库中的数据HighCharts使用。

1) Call your service, which should return proper json. 1)调用您的服务,该服务应返回正确的json。

2) On Ajax success method take the response and build the high chart. 2)在Ajax成功方法上,获取响应并构建高图表。

Your debit_amount and credit_amount are not a correct array. 您的debit_amountcredit_amount不是正确的数组。

        var company = ['Ford', 'Toyota', 'Suzuki', 'Opel', 'BMW', 'Mercedes'];
        var debit_amount = [['AAA', 34.03], ['BBB', 27.01], ['CCC', 18.77], ['DDD', 11.01], ['EEE', 5.91], ['FFF', 3.27]];
        var credit_amount = [6, 8, 2, 46, 57, 76];

        $('#account_details_chart').highcharts({
            chart: { type: 'areaspline' },
            legend: {
                layout: 'inline-block',
                align: 'right',
                verticalAlign: 'top',
                x: 0,
                y: 10,
                floating: true,
                borderWidth: 1,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
            },
            xAxis: {
                categories: company,
                plotBands: [{ // visualize the weekend
                    from: 6.5,
                    to: 6.5,
                    color: 'rgba(68, 170, 213, .2)'
                }]
            },
            yAxis: {
                title: {
                    text: 'Amount In Rupees'
                }
            },
            tooltip: {
                shared: true,
                valuePrefix: 'Rs. ',
                valueSuffix: ' /-'
            },
            credits: {
                enabled: true
            },
            plotOptions: {
                areaspline: {
                    fillOpacity: 0.5
                }
            },
            series: [{
                name: 'Debit Amount',
                data: debit_amount
            }, {
                name: 'Credit Amount',
                data: credit_amount
            }]
        });

You can create debit_amount in the same way as credit_amount, but then you won't have labels on each point on the chart. 您可以采用与credit_amount相同的方式创建debit_amount,但是这样,您就不会在图表的每个点上都具有标签。

And you don't need to write a string that pushes the values of an array in javascript. 而且您不需要编写一个字符串即可在javascript中推送数组的值。 You can just write the complete string in array format. 您可以以数组格式编写完整的字符串。

        StringBuilder sb2 = new StringBuilder();
        sb2.Append("var debit_amount = [");
        foreach (int str2 in DebitAmount)
        {
            sb2.Append("['AAA', " + str2 + "],");
        }
        Literal1.Text = sb2.ToString().TrimEnd(',') + "];";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM