根据MVC3的Highchart条形图中的值更改条形颜色

时间:2012-05-04 14:18:28

标签: c# asp.net-mvc asp.net-mvc-3 charts highcharts

我正在使用Dotnet Highchart和MVC3

我目前正在使用如下图:

Diagram

我正在尝试修改我的代码,这样我就可以根据它们的数字改变条形图上的颜色。我也想知道如何删除按钮“Snittbetyg”,如图所示。

这是我的代码:

 public ActionResult OfficeStatistic()
        {
            {

                Highcharts chart1 = new Highcharts("chart1")
                    .SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
                    .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
                    .SetSeries(new Series { Data = new Data(new object[] { 1, 8, 9, 6 }), Name = "Snittbetyg" })
                    .SetTitle(new Title { Text = "Örebro Statistik" })
                    .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column });

                return View(chart1);

            }

        }

感谢任何形式的帮助。

提前致谢!

1 个答案:

答案 0 :(得分:7)

我没有使用过Highchart,但您可以从codeplex page下载示例。看起来您的要求很容易实现。

删除“Snittbetyg”按钮

禁用图例:

.SetLegend(new Legend { Enabled = false });


添加颜色

对于系列数据使用点而不仅仅是数字:

Data data = new Data(new[]
{
    new Point { Y = 1, Color = System.Drawing.Color.Red },
    new Point { Y = 8, Color = System.Drawing.Color.Blue },
    new Point { Y = 9, Color = System.Drawing.Color.Green },
    new Point { Y = 6, Color = System.Drawing.Color.Black }
});

Highcharts chart1 = new Highcharts("chart1")
    .SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
    .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
    .SetSeries(new Series { Data = data, Name = "Snittbetyg" })
    .SetTitle(new Title { Text = "Örebro Statistik" })
    .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
    .SetLegend(new Legend { Enabled = false });

似乎没有一种内置的方法可以让highchart根据y值自动为条形颜色。我相信你必须亲自挑选颜色,例如:

    private System.Drawing.Color GetBarColour(int value)
    {
        if (value < 5) return System.Drawing.Color.Red;
        if (value > 7) return System.Drawing.Color.Green;
        return System.Drawing.Color.Orange;
    }

    public ActionResult OfficeStatistic()
    {
        {
            var dataItems = new[] {1, 8, 9, 6};
            Data data = new Data(
                dataItems.Select(y => new Point {Color = GetBarColour(y), Y = y}).ToArray()
            );

            Highcharts chart1 = new Highcharts("chart1")
                .SetXAxis(new XAxis { Categories = new[] { "Ödmjukhet", "Engagemang", "Kompetens", "Lönsamhet" } })
                .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Betygskalan" } })
                .SetSeries(new Series { Data = data, Name = "Snittbetyg" })
                .SetTitle(new Title { Text = "Örebro Statistik" })
                .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
                .SetLegend(new Legend { Enabled = false });