2017-02-05 5 views
1

各データポイントの色をそれぞれ変更することはできません。条件に基づいてASP.NET Chart DataPointをカスタマイズします

数値が< = 10の場合、色は赤色になります。 number> 10の場合、色は緑色になります。

コード(私はループのための、しかし無駄にその後、foreachループを使用してみました...) だけで利用可能//をご覧ください:

 ChartClass.Series.Clear(); 

     BedsBLL get = new BedsBLL(); 

     int A1Available = get.countAvailA1(); 
     int A1Alloted = get.countUnavailA1(); 
     int B1Available = get.countAvailB1(); 
     int B1Alloted = get.countUnavailB1(); 
     int B2Available = get.countAvailB2(); 
     int B2Alloted = get.countUnavailB2(); 
     int C1Available = get.countAvailC1(); 
     int C1Alloted = get.countUnavailC1(); 

     //Available 
     Series seriesAvail = ChartClass.Series.Add("SeriesAvailable"); 
     seriesAvail.Color = Color.ForestGreen; 
     seriesAvail.LegendText = "Available Number of Beds"; 

     String[] classArrAvail = { "A1", "B1", "B2", "C1" }; 
     int[] countAvailable = { A1Available, B1Available, B2Available, C1Available }; 

     ChartClass.Series["SeriesAvailable"].Points.DataBindXY(classArrAvail, countAvailable); 

     ChartClass.Series["SeriesAvailable"].YValuesPerPoint = 2; 

     foreach (DataPoint pt in ChartClass.Series["SeriesAvailable"].Points) 
     { 
      if (pt.XValue <= 10) 
      { 
       pt.Color = Color.Red; 
      } 
      else if (pt.XValue > 10) 
      { 
       pt.Color = Color.ForestGreen; 
      } 

      /*for (int i = 0; i < countAvailable.Length; i++) 
      { 
       if (countAvailable[i] <= 10) 
       { 
        pt.Color = Color.Red; 
       } 
       else if (countAvailable[i] > 10) 
       { 
        pt.Color = Color.ForestGreen; 
       } 
      }*/ 
     } 

     //Alloted 
     Series seriesAlloted = ChartClass.Series.Add("SeriesAlloted"); 
     seriesAlloted.Color = Color.Gray; 
     seriesAlloted.LegendText = "Alloted Number of Beds"; 

     String[] classArrAlloted = { "A1", "B1", "B2", "C1" }; 
     int[] countAlloted = { A1Alloted, B1Alloted, B2Alloted, C1Alloted }; 

     ChartClass.Series["SeriesAlloted"].Points.DataBindXY(classArrAlloted, countAlloted); 

enter image description here

デザイナー:

   <asp:Chart ID="ChartClass" runat="server" Height="350px" Width="380px"> 
       <Series> 
        <asp:Series Name="SeriesAvailable" IsValueShownAsLabel="True" LabelAngle="-90" Font="Microsoft Sans Serif, 12pt" Legend="LegendClass" ChartArea="ChartAreaClass" ChartType="StackedColumn"> 
         <SmartLabelStyle Enabled="false" /> 
        </asp:Series> 
        <asp:Series Name="SeriesAlloted" IsValueShownAsLabel="True" LabelAngle="-90" Font="Microsoft Sans Serif, 12pt" Legend="LegendClass" ChartArea="ChartAreaClass" ChartType="StackedColumn"> 
         <SmartLabelStyle Enabled="false"/> 
        </asp:Series> 
       </Series> 
       <ChartAreas> 
        <asp:ChartArea Name="ChartAreaClass"> 
         <AxisX Title="Class"> 
          <MajorGrid Enabled="false" /> 
         </AxisX> 
         <AxisY Title="Number of Beds"> 
          <MajorGrid Enabled="false" /> 
         </AxisY> 
        </asp:ChartArea> 
       </ChartAreas> 
       <Legends> 
        <asp:Legend Docking="Bottom" Name="LegendClass"></asp:Legend> 
       </Legends> 
       <Titles> 
        <asp:Title Name="TitleChart" Font="Microsoft Sans Serif, 15pt, style=Bold" Text="Beds Statistics Summary (Class)" Alignment="TopCenter"></asp:Title> 
       </Titles> 
       </asp:Chart> 
+0

が、私は同様の質問に答え、取ります一見[ここ](http://stackoverflow.com/questions/3880) 7084/how-to-customize-asp-net-chart-databound-to-sqldatasource/38811378#38811378)。 – jsanalytics

+0

しかし、なぜ私はランダムが必要ですか?鉱山は、その条件を満たすデータポイントの色を変更する条件に基づいています。申し訳ありませんが、失われています..私はasp.netを開始して以来、今まで2つのチャートを行っているだけです... @jstreet – domster

+0

あなたは、ランダムが必要です。 'PreRender'イベントを処理するだけです。しかし、今私は明示的にあなたのデータポイントを追加する(私のサンプルのようなデータベースからではない)ので、おそらくそれを必要としない....参照してください....デバッガを使用して、色を変更するループが正しく動作していることを確認する。 – jsanalytics

答えて

2

色を設定するループが正しくありません。ここではあなたがそれを行う必要があります方法は次のとおりです。

public partial class WebForm1 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string[] classArrAvail = { "A1", "B1", "B2", "C1" }; 
     int[] countAvailable = { 20, 5, 10, 15 }; 

     Chart1.Series["SeriesAvailable"].Points.DataBindXY(classArrAvail, countAvailable); 

     foreach (DataPoint pt in Chart1.Series["SeriesAvailable"].Points) 
     { 
      if (pt.YValues[0] <= 10) 
       pt.Color = Color.Red; 
      else pt.Color = Color.Green; 
     } 
    } 
} 

enter image description here


EDIT:が列ラベルのASPXの追加:

<asp:Chart ID="Chart1" runat="server" Height="400px" Width="600px"> 
     <series> 
      <asp:Series Name="SeriesAvailable" Font="Microsoft Sans Serif, 12pt" IsValueShownAsLabel="True" LabelAngle="-90"> 
       <SmartLabelStyle Enabled="False" /> 
      </asp:Series> 
     </series> 
     <chartareas> 
      <asp:ChartArea Name="ChartArea1"> 
       <AxisX> 
        <MajorGrid Enabled="False" /> 
       </AxisX> 
      </asp:ChartArea> 
     </chartareas> 
    </asp:Chart> 

enter image description here

+0

if elseに["SeriesAvailable"]を使用している場合はどうなりますか?また、.. Chart1.Series ["SeriesAvailable"]。Points.DataBindXY(classArrAvail、countAvailable);ありがとうございました.. – domster

+0

代わりに[0]を使用しました。私が間違っていると私を修正できますか? 0は、デザイナーでチャートに追加した最初のシリーズを表します。 1は、あなたが追加した2番目のシリーズを表しています。 @jstreet – domster

+0

あなたのシリーズ名をそのまま残すことができます。私は私の例で私のシリーズに名前を付けなかったので、 '[0]'を使用しています。 – jsanalytics

関連する問題