代码之家  ›  专栏  ›  技术社区  ›  reprogrammer

如何配置柱状图的颜色?

  •  1
  • reprogrammer  · 技术社区  · 15 年前

    我正在寻找一种自定义柱状图的方法。Open office和Excel为值为1、2、3、3、2的列生成以下图表。但是,我想生成具有以下属性的图表。

    1. 所有钢筋的高度必须相同。
    2. 图表应根据其值为条形图上色。在本例中,图表应使用三种颜色,因为有三个不同的值。

    如果你知道任何其他软件包可以自动生成这样的图表,我很乐意尝试一下。

    Column Chart for 1, 2, 3, 3, 2

    1 回复  |  直到 6 年前
        1
  •  2
  •   Leniel Maccaferri    15 年前

    在Excel中,您无法通过简单的步骤完成此操作。Excel中的唯一选项是手动更改每列的颜色,或按点更改颜色 here VBA 代码你可以到达那里。

    我建议使用 Microsoft ASP.NET built-in chart control

    编辑:

    这是aspx页面代码:

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
    
    <%@ Register assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.DataVisualization.Charting" tagprefix="asp" %>
    
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    
        <asp:Chart ID="Chart1" runat="server" Width="300px">
        <Series>
            <asp:Series Name="Series1" ChartArea="ChartArea1" MarkerSize="1">
                <Points>
                    <asp:DataPoint XValue="1" YValues="1" />
                    <asp:DataPoint XValue="2" YValues="2" />
                    <asp:DataPoint XValue="3" YValues="3" />
                    <asp:DataPoint XValue="4" YValues="3" />
                    <asp:DataPoint XValue="5" YValues="2" />
                </Points>
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1">
            <AxisX Interval = "1"></AxisX>
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>    
    
    </asp:Content>
    

    这是我实现的代码背后的代码-不是防弹的,因为它需要更多的测试。。。

    public partial class _Default : System.Web.UI.Page
    {
        private static Dictionary<System.Drawing.Color, double> dictionary =
            new System.Collections.Generic.Dictionary<System.Drawing.Color, double>();
    
        private Color CreateRandomColor()
        {
            Random randonGen = new Random();
    
            Color randomColor = Color.FromArgb(randonGen.Next(255), randonGen.Next(255), randonGen.Next(255));
    
            return randomColor;
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            FormatChart();
        }
    
        private bool IsColorUsed(Color color)
        {
            return dictionary.Any(kvp => kvp.Key == color);
        }
    
        private void FormatChart()
        {
            foreach (var point in Chart1.Series[0].Points)
            {
                // Point with same Y value already exist?
                var sameYValue = dictionary.Any(kvp => kvp.Value == point.YValues.First());
    
                if (sameYValue)
                {
                    //Getting the Y point...
                    var yValue = dictionary.FirstOrDefault(kvp => kvp.Value == point.YValues.First());
    
                    // Applying same color...
                    point.Color = yValue.Key;
                }
                else // Different Y value
                {
                    Color color = CreateRandomColor();
    
                    // Getting a new Color that isn't used yet...
                    while (IsColorUsed(color))
                    {
                        color = CreateRandomColor();
                    }
    
                    point.Color = color;
    
                    dictionary.Add(color, point.XValue);
                }
            }
        }
    }
    

    alt text http://www.freeimagehosting.net/uploads/22d240b0e0.png

    推荐文章