asp.net – 如何在MVC 3中设置图表系列颜色?

我在用着

System.Web.Helpers.Chart

在我的MVC3应用程序中显示图表.

@{
    var myChart = new Chart(width: 600,height: 400)
        .AddTitle("Resource utilization in Projects in Week 1")
        .AddSeries(
            name: "Project1",chartType: "StackedColumn",xValue: new[] { "W1","W2","W3","W4","W5" },yValues: new[] { 80,60,40,20,10}
        )
        .AddSeries(
            name: "Project2",yValues: new[] { 10,10,10 }
        )
        .AddSeries(
            name: "Available",yValues: new[] { "10","30","50","70","80" }
        )
        .AddLegend();        

        myChart.Write();
}

然而,系列的颜色由图表上有多少系列随机挑选.有没有人知道如何设定特定系列的特定颜色?

我在线发现Charting样本来设置颜色,但是它们正在使用命名空间

System.Web.UI.DataVisualization.Charting

解决方法

如果要自定义图表,则需要创建ChartTheme.不幸的是,这些看起来有点黑客…

例如.尝试设置这样的主题

var myChart = new Chart(width: 600,height: 400,theme: ChartTheme.Green)

你会注意到你的图表看起来不一样.如果您点击ChartTheme.Green,然后按F12(转到定义),您将看到ChartTheme类中有大量字符串,用于定义图表的风格:

public const string Blue = @"<Chart BackColor=""#D3DFF0"" BackGradientStyle=""TopBottom"" BackSecondaryColor=""White"" BorderColor=""26,59,105"" BorderlineDashStyle=""Solid"" BorderWidth=""2"" Palette=""BrightPastel"">
    <ChartAreas>
        <ChartArea Name=""Default"" _Template_=""All"" BackColor=""64,165,191,228"" BackGradientStyle=""TopBottom"" BackSecondaryColor=""White"" BorderColor=""64,64,64"" BorderDashStyle=""Solid"" ShadowColor=""Transparent"" /> 
    </ChartAreas>
    <Legends>
        <Legend _Template_=""All"" BackColor=""Transparent"" Font=""Trebuchet MS,8.25pt,style=Bold"" IsTextAutoFit=""False"" /> 
    </Legends>
    <BorderSkin SkinStyle=""emboss"" /> 
  </Chart>";

你可以在这个XML中定制大量的东西(为什么XML?我不知道!),尽管你使用的图表类型会影响你可以做的许多工作.您可以在这里找到文档:

http://msdn.microsoft.com/en-us/library/dd456696.aspx

编辑:此链接也可能有用:

New asp.net charting controls – will they work with MVC (eventually)?

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....