我做了一个简单的代码,我已经为每个自定义轴实现了相同的缩放比例,并将所有坐标轴自动放置在正确的位置。我想你可以使用类似的代码为未来努力实现你想要的:
public Form1()
{
InitializeComponent();
InitializeChart();
}
private DataSet GetData()
{
DataSet TeeDataSet = new DataSet();
DateTime dt = DateTime.Today;
DataTable TeeDataTable = new DataTable("DataTable1");
DataColumn xval = new DataColumn("DateTime", typeof(DateTime));
DataColumn yval = new DataColumn("SystemName", typeof(double));
TeeDataTable.Columns.Add(xval);
TeeDataTable.Columns.Add(yval);
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
DataRow newRow = TeeDataTable.NewRow();
newRow[xval] = dt;
newRow[yval] = rnd.Next(100);
TeeDataTable.Rows.Add(newRow);
dt = dt.AddMonths(1);
}
TeeDataSet.Tables.Add(TeeDataTable);
return TeeDataSet;
}
private void InitializeChart()
{
tChart1.Aspect.View3D = false;
tChart1.Header.Visible = false;
tChart1.Legend.Alignment = LegendAlignments.Bottom;
tChart1.Legend.CheckBoxes = true;
for (int i = 0; i < 5; i++)
{
new Steema.TeeChart.Styles.Line(tChart1.Chart);
tChart1[i].Title = "SystemName";
tChart1[i].DataSource = GetData();//Add values using DataSource
tChart1[i].XValues.DataMember = "DateTime";
tChart1[i].XValues.DateTime = true;
tChart1[i].XValues.Order = Steema.TeeChart.Styles.ValueListOrder.Ascending;
tChart1[i].YValues.DataMember = "SystemName";
tChart1.Axes.Custom.Add(new Steema.TeeChart.Axis(tChart1.Chart));
tChart1[i].CustomVertAxis = tChart1.Axes.Custom[i];
tChart1.Axes.Custom[i].AxisPen.Color = tChart1[i].Color;
tChart1.Axes.Custom[i].Grid.Visible = false;
tChart1.Axes.Custom[i].PositionUnits = PositionUnits.Pixels;
}
tChart1.Panel.MarginUnits = PanelMarginUnits.Pixels;
tChart1.Panel.MarginTop = 20;
tChart1.Draw();
PlaceAxes(0, 0, 0, 0, 0);
tChart1.AfterDraw += new PaintChartEventHandler(tChart1_AfterDraw);
tChart1.ClickLegend += new MouseEventHandler(tChart1_ClickLegend);
tChart1.Draw();
}
void tChart1_ClickLegend(object sender, MouseEventArgs e)
{
tChart1.Draw();
}
void tChart1_AfterDraw(object sender, Graphics3D g)
{
PlaceAxes(0, 0, 0, 0, 0);
}
private void PlaceAxes(int nSeries, int NextXLeft, int NextXRight, int MargLeft, int MargRight)
{
const int extraPos = 12;
const int extraMargin = 60;
//Variable
int MaxLabelsWidth;
int lenghtTicks;
int extraSpaceBetweenTitleAndLabels;
foreach (Steema.TeeChart.Styles.Line s in tChart1.Series)
{
if (s.Active)
{
s.CustomVertAxis.Visible = true;
s.CustomVertAxis.SetMinMax(tChart1[0].YValues.Minimum, tChart1[0].YValues.Maximum);
MaxLabelsWidth = s.CustomVertAxis.MaxLabelsWidth();
lenghtTicks = s.CustomVertAxis.Ticks.Length;
extraSpaceBetweenTitleAndLabels = (s.CustomVertAxis.Title.Width);//- tChart1.Axes.Custom[nSeries].MaxLabelsWidth());
if (s.CustomVertAxis.Title.Visible)
{
s.CustomVertAxis.RelativePosition = NextXLeft;
NextXLeft = NextXLeft - (MaxLabelsWidth + lenghtTicks + extraSpaceBetweenTitleAndLabels + extraPos);
MargLeft = MargLeft + extraMargin;
}
else
{
s.CustomVertAxis.RelativePosition = NextXLeft;
NextXLeft = NextXLeft - (MaxLabelsWidth + lenghtTicks + extraPos);
MargLeft = MargLeft + extraMargin;
}
tChart1.Panel.MarginLeft = MargLeft;
tChart1.Panel.MarginRight = MargRight;
}
else
{
s.CustomVertAxis.Visible = false;
}
}
}
你能告诉我们,如果前面的代码在你的最终作品?如果您有任何问题,请告诉我。
我希望能帮上忙。
谢谢,
感谢您的重播。我没有使用数据库,我直接从传感器获取数据并更新图表。如果我有三个传感器连接,那么它将为这三个分配的颜色创建轴。我没有使用你的代码来绘制轴。上面的代码我发布了完美的工作。我已经添加了一点点代码来初始化图表可能是你可以看到的。如果你需要更多的信息,请问我。 – reddy 2013-03-05 13:35:01
感谢您的信息。您对我所做的修改对我没有帮助,但是我修改了我的代码,因为系列从DataSet获取数据,并且对我来说工作正常。我认为你可以使用我在代码中添加的一些条件,方法或属性,并将它应用到你的代码中,以达到最终的效果。如果我的建议代码不能帮助您,请将您的问题发送给我们一个简单的示例代码,因为我们可以在这里准确复制您的问题并尝试为您找到一个好的解决方案。谢谢, – 2013-03-06 13:35:44
你能否知道我需要用哪个属性来改变轴缩放字母的颜色。 – reddy 2013-03-07 10:10:16