2010-07-11 90 views
1

我正在尋找一種方法來自定義列圖表。開放式辦公室和Excel爲值爲1,2,3,3,2的列生成以下圖表。但是,我想生成具有以下屬性的圖表。如何配置柱形圖的着色?

  1. 圖表應該有五個小節。
  2. 所有酒吧必須是相同的高度。
  3. 圖表應該根據顏色條的值進行顏色顯示。在這個例子中,圖表應該使用三種顏色,因爲有三種不同的值。

如果你知道任何其他軟件包可以自動生成這樣的圖表,我很樂意嘗試一下。

Column Chart for 1, 2, 3, 3, 2 http://i29.tinypic.com/kclcvn.jpg

+0

你爲什麼要條形圖如果「所有酒吧必須是相同的高度。」 ?? – 2010-07-12 02:53:42

回答

2

在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

+0

我不是一個.NET程序員。但是,請隨時爲.NET用戶發佈解決方案。 – reprogrammer 2010-07-11 03:12:04