C#MVC - 从变量图形中环条形图 [英] C# MVC - Drawing Bar Chart from Variables within Loop

查看:89
本文介绍了C#MVC - 从变量图形中环条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是应对:
<一href=\"http://stackoverflow.com/questions/9685302/showing-system-web-helpers-chart-in-a-partial-view-from-the-model/35013576?noredirect=1#comment57754057_35013576\">Showing System.Web.Helpers.Chart在从该模型的局部视图

My question is in response to: Showing System.Web.Helpers.Chart in a partial view from the model

@{
    var myChart = new Chart(width: 600, height: 400)
        .AddTitle("Chart Title")
        .AddSeries(
            name: "Employee",
            xValue: new[] {  "Peter", "Andrew", "Julie", "Mary", "Dave" },
            yValues: new[] { "2", "6", "4", "5", "3" })
        .Write();
}

有没有人能够从与xValue和y值内循环使用的值。目前在例子,我发现所有的价值观都难以codeD。

Has anyone been able to use values from a loop within the xValue and yValue. Currently in the examples I have found all the values are hard coded.

我想用item.season作为X和item.year设为Y从下面的foreach语句(首先收集主体,收集第二年,季):

I would like to use the item.season as the X and item.year as the Y from the foreach statement below(first gather subject, second gathers year and season):

string json2 = File.ReadAllText(@"C:\Temp\Assess.json");
var root = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<WebApplication1.Models.SchoolSubject>>(json2);
        foreach (var subject in root)
        {
            <h2>@Html.Raw(subject.Subject)</h2>
            foreach (var item in subject.AppScores)
            {
               <p>@Html.Raw(item.Season)</p> 
                <p>@Html.Raw(item.year)</p>
                <p>@Html.Raw(item.value)</p>
                }
            }

与类code型号包含:

Model with Class Code contained:

public class SchoolSubjectAppScore
    {
        public string Season { get; set; }
        public string year { get; set; }
        public string value { get; set; }
    }
    public class SchoolSubject
    {
        public SchoolSubject() { this.AppScores = new List<SchoolSubjectAppScore>(); }
        public string Subject { get; set; }
        public List<SchoolSubjectAppScore> AppScores { get; set; }
    }

JSON文件我已经是:

The JSON file I have is:

[{"Subject": "English","AppScores": [{"Season": "Winter","year": "2009", "value" : "20" }, {"Season": "Summer","year": "2009", "value" : "38"}]}, {"Subject": "Maths","AppScores": [{"Season": "Winter","year": "2009", "value" : "10"}, {"Season": "Summer","year": "2009", "value" : "27"}]}]

这是我想的图形输出是两个系列 - 数学/英语,是xValue - 季/年和y值 - 值

The output on the graph I would like is two Series - Maths/English, the xValue - Season/Year and the yValue - Value.

任何想法,我到处找这个帮助。

Any ideas as I have look everywhere for help on this.

推荐答案

这是很容易。你只需要添加季节从JSON到列表数据当你在循环。一旦你的列表您可以将其转换为一个数组,并将其分配给图表。请参见下面更新code。

That is easy. You just need to add your Season and year data from json to a List when you are in the Loop. Once you have the List you can convert it to an array and assign it to the Chart. See updated code below.

string json2 = File.ReadAllText(@"C:\Temp\Assess.json");
var root = new System.Web.Script.Serialization.JavaScriptSerialize().Deserialize<List<WebApplication1.Models.SchoolSubject>>(json2);

var seasons = new List<string>();
var years = new List<string>();

    foreach (var subject in root)
    {
        <h2>@Html.Raw(subject.Subject)</h2>
        foreach (var item in subject.AppScores)
        {
            <p>@Html.Raw(item.Season)</p> 
            <p>@Html.Raw(item.year)</p>

            seasons.Add(item.Season);
            years.Add(item.year);
            }
        }

在做上面可以在季节添加到图之后。

@{
var myChart = new Chart(width: 600, height: 400)
    .AddTitle("Chart Title")
    .AddSeries(
        name: "Employee",
        xValue: seasons.ToArray(),
        yValues: years.ToArray())
    .Write();
}

这篇关于C#MVC - 从变量图形中环条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆