如何在Winform图表上添加尖峰到线? [英] How to add spikes to line on Winform chart?

查看:190
本文介绍了如何在Winform图表上添加尖峰到线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从一个文本文件中读取的数字上绘制一条线。文件的每一行上有一个对应于X坐标的数字,而Y坐标是它所在的行。



现在的要求已经改为包括特殊事件,其中如果行上的数字后面跟着特殊的字,尖峰将出现如下图所示:



目前我可以找到的唯一方法是为每个尖峰使用一行,但是可能有一个大的这些特殊事件,因此需要模块化。这似乎是一个有效和坏的方式来编程。



是否可以将尖峰添加到同一图线?或者是可以只使用一个额外的行,并使它断开(不可见),并只显示峰值是要看到哪里?



我看过使用bar

解决方案

DataPoints c < 已连接,因此无法真正破解分开。但是每个导致 DataPoint 的段可以有自己的颜色,并且包括 Color.Transparent



无需添加额外系列注释 ,你的两个问题可以这样解决:




  • 为了简单地添加'spikes'所有你需要做的是插入2个合适的数据点,第二个与尖峰连接的点相同。


  • 要添加未连接的线,您需要通过添加一个透明颜色的点来跳转



/ p>

下面是两个示例方法:

  void addSpike int index,double spikeWidth)
{
DataPoint dp = s.Points [index];
DataPoint dp1 = new DataPoint(dp.XValue + spikeWidth,dp.YValues [0]);

s.Points.Insert(index + 1,dp1);
s.Points.Insert(index + 2,dp);
}


void addLine(Series s,int index,double spikeDist,double spikeWidth)
{
DataPoint dp = s.Points [index] ;
DataPoint dp1 = new DataPoint(dp.XValue + spikeDist,dp.YValues [0]);
DataPoint dp2 = new DataPoint(dp.XValue + spikeWidth,dp.YValues [0]);
DataPoint dp0 = dp.Clone();

dp1.Color = Color.Transparent;
dp2.Color = dp.Color;
dp2.BorderWidth = 2; // optional
dp0.Color = Color.Transparent;

s.Points.Insert(index + 1,dp1);
s.Points.Insert(index + 2,dp2);
s.Points.Insert(index + 3,dp0);
}

您可以这样调用:

  addSpike(chart1.Series [0],3,50d); 
addLine(chart1.Series [0],6,30d,80d);

请注意,他们向Points集合添加2或3个DataPoints!



当然,您可以根据需要设置额外行的颜色和宽度(又称为BorderWidth),并将它们包括在params列表中。



如果你想保持点集合不变,你也可以简单地创建一个尖峰系列,并在那里添加尖峰点。诀窍是用一条透明的线'跳'到新点!


I am drawing a line on a graph from numbers read from a text file. There is a number on each line of the file which corresponds to the X co-ordinate while the Y co-ordinate is the line it is on.

The requirements have now changed to include "special events" where if the number on the line is followed by the word special a spike will appear like image below:

Currently the only way I can find is to use a line for each spike, however there could be a large of these special events and so needs to be modular. This seems an efficient and bad way to program it.

Is it possible to add the spikes to the same graph line? Or is it possible to use just one additional line and have it broken (invisible) and only show where the spikes are meant to be seen?

I have looked at using bar graphs but due to other items on the graph I cannot.

解决方案

The DataPoints of a Line Chart are connected so it is not possble to really break it apart. However each segment leading to a DataPoint can have its own color and that includes Color.Transparent which lends itself to a simple trick..

Without adding extra Series or Annotations, your two questions can be solved like this:

  • To simply add the 'spikes' you show us in the 2nd graph, all you need to do is to insert 2 suitable datapoints, the 2nd being identical to the point the spike is connected to.

  • To add an unconnected line you need to 'jump' to its beginning by adding one extra point with a transparent color.

Here are two example methods:

void addSpike(Series s, int index, double spikeWidth)
{
    DataPoint dp = s.Points[index];
    DataPoint dp1 = new DataPoint(dp.XValue + spikeWidth, dp.YValues[0]);

    s.Points.Insert(index+1, dp1);
    s.Points.Insert(index+2, dp);
}


void addLine(Series s, int index, double spikeDist, double spikeWidth)
{
    DataPoint dp = s.Points[index];
    DataPoint dp1 = new DataPoint(dp.XValue + spikeDist, dp.YValues[0]);
    DataPoint dp2 = new DataPoint(dp.XValue + spikeWidth, dp.YValues[0]);
    DataPoint dp0 = dp.Clone();

    dp1.Color = Color.Transparent;
    dp2.Color = dp.Color;
    dp2.BorderWidth = 2;             // optional
    dp0.Color = Color.Transparent;

    s.Points.Insert(index + 1, dp1);
    s.Points.Insert(index + 2, dp2);
    s.Points.Insert(index + 3, dp0);
}

You can call them like this:

addSpike(chart1.Series[0], 3, 50d);
addLine(chart1.Series[0], 6, 30d,  80d);

Note that they add 2 or 3 DataPoints to the Points collection!

Of course you can set the Color and width (aka BorderWidth) of the extra lines as you wish and also include them in the params list..

If you want to keep the points collection unchanged you also can simply create one 'spikes series' and add the spike points there. The trick is to 'jump' to the new points with a transparent line!

这篇关于如何在Winform图表上添加尖峰到线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆