2015-08-08 93 views
1

正在創建它使用chart.I要修改圖表的Windows窗體應用程序Windows窗體圖表標籤修改

  1. 如何(在屏幕截圖白色的)編輯圖表區回地面顏色
  2. 如何爲每個小節應用不同的顏色。
  3. 如何格式化Y軸標籤(需要對齊文件擴展左端和整數值右端)

這是我目前的屏幕截圖

enter image description here

需要更新我的屏幕這樣

enter image description here

是否有可能對準標籤這樣

enter image description here

回答

2

下面是一個例子:

enter image description here

的款式,你用這個Chart

// prepare: 
chart1.Series.Clear(); 
Series S1 = chart1.Series.Add("S1"); 
ChartArea CA = chart1.ChartAreas[0]; 

// style type, font, color, axes 
S1.ChartType = SeriesChartType.Bar; 
CA.BackColor = Color.AliceBlue; 
chart1.BackColor = CA.BackColor; 
Font f = new Font("Consolas", 10f); 
CA.AxisX.LabelStyle.Font = f; 

S1.BackGradientStyle = GradientStyle.TopBottom; 
CA.AxisX.MajorGrid.Enabled = false; 
CA.AxisX.MajorTickMark.Enabled = false; 
CA.AxisX.LineColor = Color.Transparent; 

CA.AxisY.Enabled = AxisEnabled.False; 
CA.AxisY.MajorGrid.Enabled = false; 
CA.AxisY.MajorTickMark.Enabled = false; 

顏色個人DataPoints你必須將其Color

S1.Points[0].Color = Color.YellowGreen; 
S1.Points[1].Color = Color.YellowGreen; 

要創建formatted標籤,您可以創建字符串,並把它們作爲(僞)X值添加點的時候:

string label = string.Format("{0,-11}{1, 7:0.0}%{2,8:##0.0}GB ", 
       t.Item1, t.Item2 * 100d/total, t.Item2) + "\u2001\u2001"; 
int idx = S1.Points.AddXY(label, t.Item2); 

這裏我用一個Tuple<string, int>來握住我的數據。您需要將其調整爲適合您的數據源。請注意我如何從總數中計算百分比。

以下是完整的代碼,我用我的示例數據:

List<Tuple<string, double>> data = new List<Tuple<string, double>>() 
{ 
    new Tuple<string, double>("0-1 months", 4), 
    new Tuple<string, double>("2-3 months", 14), 
    new Tuple<string, double>("4-11 months", 44), 
    new Tuple<string, double>("1-2 years", 23), 
    new Tuple<string, double>("3-5 years", 3), 
    new Tuple<string, double>("> 5 years", 100), 

}; 

double total = data.Sum(x => x.Item2); 

foreach (Tuple<string, double> t in data) 
{ 
    string label = string.Format("{0,-11}{1, 7:0.0}%{2,8:##0.0}GB", 
        t.Item1, t.Item2 * 100d/total, t.Item2) + "\u2001\u2001"; 
    int i = S1.Points.AddXY(label, t.Item2); 
    S1.Points[i].Font = f; 
} 

注:

  • 爲了得到內部線對齊,你需要使用一個等寬字體Consolas
  • 另請注意,您不能在一個標籤內使用不同的字體或樣式。
  • 我添加兩個m-space characters來創建標籤和plotarea之間的距離。 (普通空間不會顯示!)。

更新:

第二圖表圖像示出了SeriesChartType.Column。要插入新行,您只需插入一個\n角色,並確保有足夠的空間。

棘手的部分是有不同的顏色一行。這是不可能的標籤。

相反,您需要爲每個數據點添加兩個CustomLabels:第一個顯示的是例如黑色線條。第二個可以有不同的ForeColor,並且在開始時需要具有儘可能多的\n,因爲第一個有行。

請注意,CustomLabels坐在兩個職位之間的中間位置。所以,我現在已經有實數增加了DataPoints,從0開始,因爲他們的X值,然後中途之間的位置CustomLabels ..

enter image description here

chart1.Series.Clear(); 
    Series S1 = chart1.Series.Add("S1"); 
    S1.ChartType = SeriesChartType.Column; 
    ChartArea CA = chart1.ChartAreas[0]; 
    chart1.Legends.Clear(); 

    CA.BackColor = Color.AliceBlue; 
    chart1.BackColor = Color.AliceBlue; 
    Font f = new Font("Consolas", 9f); 

    CA.AxisX.LabelStyle.Font = f; 

    S1.BackGradientStyle = GradientStyle.LeftRight; 
    CA.AxisX.MajorGrid.Enabled = false; 
    CA.AxisX.MajorTickMark.Enabled = false; 
    CA.AxisX.LineColor = Color.Transparent; 

    CA.AxisY.Enabled = AxisEnabled.False; 
    CA.AxisY.MajorGrid.Enabled = false; 
    CA.AxisY.MajorTickMark.Enabled = false; 

    CA.Position.X = 0f; 

    List<Tuple<string, double>> data = new List<Tuple<string, double>>() 
    { 
     new Tuple<string, double>("0-1 months", 4), 
     new Tuple<string, double>("2-3 months", 14), 
     new Tuple<string, double>("4-11 months", 44), 
     new Tuple<string, double>("1-2 years", 23), 
     new Tuple<string, double>("3-5 years", 3), 
     new Tuple<string, double>("> 5 years", 100), 

    }; 

    double total = data.Sum(x => x.Item2); 

    foreach (Tuple<string, double> t in data) 
    { 
     string label1 = string.Format("{0}\n{1:0.0}%", t.Item1, t.Item2 * 100d/total); 
     string label2 = string.Format("\n\n{0:##0.0}GB", t.Item2); 
     int i = S1.Points.AddXY(S1.Points.Count, t.Item2); 
     S1.Points[i].Font = f; 

     DataPoint dp = S1.Points[i]; 
     int v = (int)dp.YValues[0]; 
     CustomLabel cl = new CustomLabel(); 
     cl.Text = label1; 
     cl.FromPosition = i - 0.5f; 
     cl.ToPosition = i + 0.5f; 

     CustomLabel cl2 = new CustomLabel(); 
     cl2.Text = label2; 
     cl2.FromPosition = i -0.5f; 
     cl2.ToPosition = i + 0.5f; 

     cl2.ForeColor = Color.Green; 
     CA.AxisX.CustomLabels.Add(cl); 
     CA.AxisX.CustomLabels.Add(cl2); 

    } 
    S1.Points[0].Color = Color.YellowGreen; 
    S1.Points[1].Color = Color.YellowGreen; 
+0

感謝TAW。它幫了我很多... – thejustv

+0

我又添加了一個屏幕截圖,你能幫我把格式化爲這樣的標籤嗎.. – thejustv

+0

我已經添加了一些關於如何做第二張圖像的提示。 – TaW