1

フロー円グラフの作成に JSON を使用しようとしています。静的データでは正常に動作しますが、データを動的に (JSON で) 送信したい場合、何も表示されません。ここに私のコードがあります:

<script src="jquery-1.11.2.min.js"></script>
<script src="jquery.flot.js"></script>
<script src="jquery.flot.pie.js"></script>

....

$.ajax({
            url: 'PieChart.aspx/GetData',
            contentType: "application/json; charset=utf-8",
            type: "POST",
            dataType: 'json',
            success: function (data) {
                //alert("should be shown");
                $.plot($("#placeholder"), data, {
                    series: {
                        pie: {
                            show: true
                        }
                    },
                    legend: {
                        labelBoxBorderColor: "none"
                    }
                });
            },
            failure: function (response) {
                alert(response.d);
            }

        });

Asp.net server_side のコード:

 [System.Web.Services.WebMethod]
  public static string GetData()
    {
     //there are some other codes here, just to test. I've replaced them using this line. 
    return "[{ label: \"IE\", data: 19.5, color: \"#4572A7\" }, { label: \"Safari\", data: 4.5, color: \"#80699B\" }]";
    }

助けが必要。ありがとう

4

4 に答える 4

0
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ChartDemo.aspx.cs" Inherits="piecharts.ChartDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.7.1.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script>



        // Load the Visualization API and the piechart package.
        google.load('visualization', '1.0', { 'packages': ['corechart'] });


        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChart);

        // Callback that creates and populates a data table,
        // instantiates the pie chart, passes in the data and
        // draws it.

        function drawChart() {
            //alert($("#hdnJson").val());
            // Create the data table.

            var jsonObj = JSON.parse($("#hdnJson").val());
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Colour');
            data.addColumn('number', 'Total');
            data.addRows([
              [jsonObj[0].Colour, jsonObj[0].Total],
              [jsonObj[1].Colour, jsonObj[1].Total],
              [jsonObj[2].Colour, jsonObj[2].Total],
              [jsonObj[3].Colour, jsonObj[3].Total]

            ]);

            // Set chart options
            var options = {

                'width': 400,
                'height': 300,
                legend: {position: 'none'},
                'pieSliceText': 'value',
                tooltip: { text: 'value' },
                'is3D':true,
                //sliceVisibilityThreshold: 0,
                'slices': { 0: { color: 'Green' }, 1: { color: '#FFFF7F' }, 2: { color: 'silver' }, 3: { color: 'red' } }


            };

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
            chart.draw(data, options, { sliceVisibilityThreshold: 0 });
        }
        }
    </script>
</head>
于 2015-02-19T09:01:21.423 に答える
0
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace piecharts
{
    public partial class ChartDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.11.22.11)(PORT=1111)))(CONNECT_DATA=(SERVICE_NAME=mohan)));User ID=kittu;Password=kittu"))
            {
                con.Open();
                System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand("select parameter,sum(weightage) as Total from projectweightage GROUP BY parameter", con);
                System.Data.OracleClient.OracleDataAdapter da = new System.Data.OracleClient.OracleDataAdapter(cmd);
                da.Fill(dt);
                con.Close();
            }
            HiddenField hdnField = new HiddenField();
            var jsonData=ChartJson(dt);
            hdnField.Value = jsonData;
            hdnField.ID = "hdnJson";
            form1.Controls.Add(hdnField);

        }
        private string ChartJson(DataTable dt)
        {
            List<ChartDetails> lstChartDetails = new List<ChartDetails>();
            foreach (DataRow dRows in dt.Rows)
            {
                ChartDetails chartdtls = new ChartDetails();
                chartdtls.Colour = Convert.ToString(dRows["Parameter"]);
                chartdtls.Total = Convert.ToInt32(dRows["Total"]);
                lstChartDetails.Add(chartdtls);
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(lstChartDetails);

        }


    }
}
于 2015-02-19T08:54:38.273 に答える
0
*****Properties****

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace piecharts
{
    public class ChartDetails
    {
        public int Total { get; set; }
        public string Colour { get; set; }
    }
}
于 2015-02-19T09:06:05.730 に答える