如何配置柱形图的颜色?

发布于 2024-09-08 23:17:50 字数 384 浏览 9 评论 0原文

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

  1. 该图表应有五个条形。
  2. 所有条形的高度必须相同。
  3. 图表应根据条形的值为其着色。在此示例中,图表应使用三种颜色,因为存在三个不同的值。

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

柱形图对于 1, 2, 3, 3, 2

I'm looking for a way to customize a column chart. Open office and Excel produce the following chart for a column with values 1, 2, 3, 3, 2. But, I'd like to generate a chart with the following properties.

  1. The chart should have five bars.
  2. All bars must be of the same height.
  3. The chart should color bars based on their values. In this example, the chart should use three colors because there are three different values.

If you know of any other software package that can automatically generate such a chart, I'd be glad to try it out.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤ぢ 2024-09-15 23:17:50

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

我建议使用 Microsoft ASP.NET 内置图表控件。它会给你很多定制的可能性。我会尝试发布一个工作示例。

编辑:

刚刚设法获得一个工作示例:

这是 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);
            }
        }
    }
}

这是结果图表:

替代文本 http://www.freeimagehosting.net/uploads/22d240b0e0.png

In Excel you can not do this in simple steps. The only options you have in Excel is to change the color of each column manually or vary the color by point as you can see here. I think that through VBA code you can get there.

I'd recommend the use of Microsoft ASP.NET built-in chart control. It'll give you lots of customization possibilities. I'll try to post a working sample.

Edit:

Just managed to get a working sample:

This is the aspx page code:

<%@ 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>

This is the code-behind code I implemented - not bullet proof because it needs more testing...

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);
            }
        }
    }
}

This is the resulting chart:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文