作成日: 2014/09/26 最終更新日: 2014/09/26
文書種別
技術情報
詳細
正負の値が混在するデータを用いて積層型棒グラフを作成する場合、MS Excelと同じように表示するには、正のデータと負のデータでChartGroupを分ける必要があります。
(※既にChartGroupを2つ使用して複合グラフを作成している場合は、実現困難となります。)
◆サンプルコード
[VB]
[C#]
(※既にChartGroupを2つ使用して複合グラフを作成している場合は、実現困難となります。)
◆サンプルコード
[VB]
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'グラフデータを設定します
Dim cd1 As C1.Win.C1Chart.ChartData = _
C1WebChart1.ChartGroups(0).ChartData
Dim cd2 As C1.Win.C1Chart.ChartData = _
C1WebChart1.ChartGroups(1).ChartData
cd1.SeriesList.Clear()
cd2.SeriesList.Clear()
Dim y1 As Double() = {100, -40, 60}
Dim y2 As Double() = {-20, 70, 80}
Dim y3 As Double() = {50, -30, -10}
AddSeries(cd1, cd2, y1, System.Drawing.Color.LightSalmon)
AddSeries(cd1, cd2, y2, System.Drawing.Color.LightYellow)
AddSeries(cd1, cd2, y3, System.Drawing.Color.LightGreen)
'積層型棒グラフを設定します
C1WebChart1.ChartGroups(0).ChartType = _
C1.Win.C1Chart.Chart2DTypeEnum.Bar
C1WebChart1.ChartGroups(1).ChartType = _
C1.Win.C1Chart.Chart2DTypeEnum.Bar
C1WebChart1.ChartGroups(0).Stacked = True
C1WebChart1.ChartGroups(1).Stacked = True
'軸を設定します
Dim ymin As Double = cd2.MinY
Dim ymax As Double = cd1.MaxY
C1WebChart1.ChartArea.AxisY.SetMinMax(ymin, ymax)
C1WebChart1.ChartArea.AxisY2.SetMinMax(ymin, ymax)
C1WebChart1.ChartArea.AxisY2.Visible = True
'凡例表示を調整します
C1WebChart1.Legend.Visible = True
Dim scnt As Integer = _
C1WebChart1.ChartGroups(1).ChartData.SeriesList.Count
Dim i As Integer
For i = 0 To scnt - 1
C1WebChart1.ChartGroups(1).ChartData(i).LegendEntry = False
Next i
C1WebChart1.ChartGroups(0).ChartData(0).Label = "データセット1"
C1WebChart1.ChartGroups(0).ChartData(1).Label = "データセット2"
C1WebChart1.ChartGroups(0).ChartData(2).Label = "データセット3"
End Sub
'データの正負によりChartGroupを分けます
Private Sub AddSeries(ByVal cd1 As C1.Win.C1Chart.ChartData, _
ByVal cd2 As C1.Win.C1Chart.ChartData, ByVal y() As Double, _
ByVal clr As System.Drawing.Color)
Dim len As Integer = y.Length
Dim y1(len) As Double
Dim y2(len) As Double
Dim x1(len) As Double
Dim x2(len) As Double
Dim i As Integer
For i = 0 To len - 1
If y(i) >= 0 Then
y1(i) = y(i)
y2(i) = cd2.Hole
Else
y1(i) = cd1.Hole
y2(i) = y(i)
End If
x1(i) = i
x2(i) = i
Next i
Dim ds1 As C1.Win.C1Chart.ChartDataSeries = _
cd1.SeriesList.AddNewSeries()
Dim ds2 As C1.Win.C1Chart.ChartDataSeries = _
cd2.SeriesList.AddNewSeries()
ds1.X.CopyDataIn(x1)
ds2.X.CopyDataIn(x2)
ds1.Y.CopyDataIn(y1)
ds2.Y.CopyDataIn(y2)
ds1.LineStyle.Color = clr
ds2.LineStyle.Color = clr
End Sub
ByVal e As System.EventArgs) Handles Button1.Click
'グラフデータを設定します
Dim cd1 As C1.Win.C1Chart.ChartData = _
C1WebChart1.ChartGroups(0).ChartData
Dim cd2 As C1.Win.C1Chart.ChartData = _
C1WebChart1.ChartGroups(1).ChartData
cd1.SeriesList.Clear()
cd2.SeriesList.Clear()
Dim y1 As Double() = {100, -40, 60}
Dim y2 As Double() = {-20, 70, 80}
Dim y3 As Double() = {50, -30, -10}
AddSeries(cd1, cd2, y1, System.Drawing.Color.LightSalmon)
AddSeries(cd1, cd2, y2, System.Drawing.Color.LightYellow)
AddSeries(cd1, cd2, y3, System.Drawing.Color.LightGreen)
'積層型棒グラフを設定します
C1WebChart1.ChartGroups(0).ChartType = _
C1.Win.C1Chart.Chart2DTypeEnum.Bar
C1WebChart1.ChartGroups(1).ChartType = _
C1.Win.C1Chart.Chart2DTypeEnum.Bar
C1WebChart1.ChartGroups(0).Stacked = True
C1WebChart1.ChartGroups(1).Stacked = True
'軸を設定します
Dim ymin As Double = cd2.MinY
Dim ymax As Double = cd1.MaxY
C1WebChart1.ChartArea.AxisY.SetMinMax(ymin, ymax)
C1WebChart1.ChartArea.AxisY2.SetMinMax(ymin, ymax)
C1WebChart1.ChartArea.AxisY2.Visible = True
'凡例表示を調整します
C1WebChart1.Legend.Visible = True
Dim scnt As Integer = _
C1WebChart1.ChartGroups(1).ChartData.SeriesList.Count
Dim i As Integer
For i = 0 To scnt - 1
C1WebChart1.ChartGroups(1).ChartData(i).LegendEntry = False
Next i
C1WebChart1.ChartGroups(0).ChartData(0).Label = "データセット1"
C1WebChart1.ChartGroups(0).ChartData(1).Label = "データセット2"
C1WebChart1.ChartGroups(0).ChartData(2).Label = "データセット3"
End Sub
'データの正負によりChartGroupを分けます
Private Sub AddSeries(ByVal cd1 As C1.Win.C1Chart.ChartData, _
ByVal cd2 As C1.Win.C1Chart.ChartData, ByVal y() As Double, _
ByVal clr As System.Drawing.Color)
Dim len As Integer = y.Length
Dim y1(len) As Double
Dim y2(len) As Double
Dim x1(len) As Double
Dim x2(len) As Double
Dim i As Integer
For i = 0 To len - 1
If y(i) >= 0 Then
y1(i) = y(i)
y2(i) = cd2.Hole
Else
y1(i) = cd1.Hole
y2(i) = y(i)
End If
x1(i) = i
x2(i) = i
Next i
Dim ds1 As C1.Win.C1Chart.ChartDataSeries = _
cd1.SeriesList.AddNewSeries()
Dim ds2 As C1.Win.C1Chart.ChartDataSeries = _
cd2.SeriesList.AddNewSeries()
ds1.X.CopyDataIn(x1)
ds2.X.CopyDataIn(x2)
ds1.Y.CopyDataIn(y1)
ds2.Y.CopyDataIn(y2)
ds1.LineStyle.Color = clr
ds2.LineStyle.Color = clr
End Sub
[C#]
protected void button1_Click(object sender, EventArgs e)
{
//グラフデータを設定します
C1.Win.C1Chart.ChartData cd1 =
C1WebChart1.ChartGroups[0].ChartData;
C1.Win.C1Chart.ChartData cd2 =
C1WebChart1.ChartGroups[1].ChartData;
cd1.SeriesList.Clear();
cd2.SeriesList.Clear();
double[] y1 = { 100, -40, 60 };
double[] y2 = { -20, 70, 80 };
double[] y3 = { 50, -30, -10 };
AddSeries(cd1, cd2, y1, System.Drawing.Color.LightSalmon);
AddSeries(cd1, cd2, y2, System.Drawing.Color.LightYellow);
AddSeries(cd1, cd2, y3, System.Drawing.Color.LightGreen);
//積層型棒グラフを設定します
C1WebChart1.ChartGroups[0].ChartType =
C1.Win.C1Chart.Chart2DTypeEnum.Bar;
C1WebChart1.ChartGroups[1].ChartType =
C1.Win.C1Chart.Chart2DTypeEnum.Bar;
C1WebChart1.ChartGroups[0].Stacked = true;
C1WebChart1.ChartGroups[1].Stacked = true;
//軸を設定します
double ymin = cd2.MinY;
double ymax = cd1.MaxY;
C1WebChart1.ChartArea.AxisY.SetMinMax(ymin, ymax);
C1WebChart1.ChartArea.AxisY2.SetMinMax(ymin, ymax);
C1WebChart1.ChartArea.AxisY2.Visible = true;
//凡例表示を調整します
C1WebChart1.Legend.Visible = true;
int scnt = C1WebChart1.ChartGroups[1].ChartData.SeriesList.Count;
for (int i = 0; i < scnt; i++)
{
C1WebChart1.ChartGroups[1].ChartData[i].LegendEntry = false;
}
C1WebChart1.ChartGroups[0].ChartData[0].Label = "データセット1";
C1WebChart1.ChartGroups[0].ChartData[1].Label = "データセット2";
C1WebChart1.ChartGroups[0].ChartData[2].Label = "データセット3";
}
//データの正負によりChartGroupを分けます
private void AddSeries(C1.Win.C1Chart.ChartData cd1,
C1.Win.C1Chart.ChartData cd2, double[] y, System.Drawing.Color clr)
{
int len = y.Length;
double[] y1 = new double[len];
double[] y2 = new double[len];
double[] x1 = new double[len];
double[] x2 = new double[len];
for (int i = 0; i < len; i++)
{
if (y[i] >= 0)
{
y1[i] = y[i];
y2[i] = cd2.Hole;
}
else
{
y1[i] = cd1.Hole;
y2[i] = y[i];
}
x1[i] = i;
x2[i] = i;
}
C1.Win.C1Chart.ChartDataSeries ds1 =
cd1.SeriesList.AddNewSeries();
C1.Win.C1Chart.ChartDataSeries ds2 =
cd2.SeriesList.AddNewSeries();
ds1.X.CopyDataIn(x1);
ds2.X.CopyDataIn(x2);
ds1.Y.CopyDataIn(y1);
ds2.Y.CopyDataIn(y2);
ds1.LineStyle.Color = clr;
ds2.LineStyle.Color = clr;
}
{
//グラフデータを設定します
C1.Win.C1Chart.ChartData cd1 =
C1WebChart1.ChartGroups[0].ChartData;
C1.Win.C1Chart.ChartData cd2 =
C1WebChart1.ChartGroups[1].ChartData;
cd1.SeriesList.Clear();
cd2.SeriesList.Clear();
double[] y1 = { 100, -40, 60 };
double[] y2 = { -20, 70, 80 };
double[] y3 = { 50, -30, -10 };
AddSeries(cd1, cd2, y1, System.Drawing.Color.LightSalmon);
AddSeries(cd1, cd2, y2, System.Drawing.Color.LightYellow);
AddSeries(cd1, cd2, y3, System.Drawing.Color.LightGreen);
//積層型棒グラフを設定します
C1WebChart1.ChartGroups[0].ChartType =
C1.Win.C1Chart.Chart2DTypeEnum.Bar;
C1WebChart1.ChartGroups[1].ChartType =
C1.Win.C1Chart.Chart2DTypeEnum.Bar;
C1WebChart1.ChartGroups[0].Stacked = true;
C1WebChart1.ChartGroups[1].Stacked = true;
//軸を設定します
double ymin = cd2.MinY;
double ymax = cd1.MaxY;
C1WebChart1.ChartArea.AxisY.SetMinMax(ymin, ymax);
C1WebChart1.ChartArea.AxisY2.SetMinMax(ymin, ymax);
C1WebChart1.ChartArea.AxisY2.Visible = true;
//凡例表示を調整します
C1WebChart1.Legend.Visible = true;
int scnt = C1WebChart1.ChartGroups[1].ChartData.SeriesList.Count;
for (int i = 0; i < scnt; i++)
{
C1WebChart1.ChartGroups[1].ChartData[i].LegendEntry = false;
}
C1WebChart1.ChartGroups[0].ChartData[0].Label = "データセット1";
C1WebChart1.ChartGroups[0].ChartData[1].Label = "データセット2";
C1WebChart1.ChartGroups[0].ChartData[2].Label = "データセット3";
}
//データの正負によりChartGroupを分けます
private void AddSeries(C1.Win.C1Chart.ChartData cd1,
C1.Win.C1Chart.ChartData cd2, double[] y, System.Drawing.Color clr)
{
int len = y.Length;
double[] y1 = new double[len];
double[] y2 = new double[len];
double[] x1 = new double[len];
double[] x2 = new double[len];
for (int i = 0; i < len; i++)
{
if (y[i] >= 0)
{
y1[i] = y[i];
y2[i] = cd2.Hole;
}
else
{
y1[i] = cd1.Hole;
y2[i] = y[i];
}
x1[i] = i;
x2[i] = i;
}
C1.Win.C1Chart.ChartDataSeries ds1 =
cd1.SeriesList.AddNewSeries();
C1.Win.C1Chart.ChartDataSeries ds2 =
cd2.SeriesList.AddNewSeries();
ds1.X.CopyDataIn(x1);
ds2.X.CopyDataIn(x2);
ds1.Y.CopyDataIn(y1);
ds2.Y.CopyDataIn(y2);
ds1.LineStyle.Color = clr;
ds2.LineStyle.Color = clr;
}
旧文書番号
69791