/
Form1.cs
84 lines (79 loc) · 3 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
namespace ChartLineColor {
public partial class Form1 : Form {
double level = 0;
double maxPointValue = 0;
double minPointValue = 0;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
chartControl1.BoundDataChanged += OnBoundDataChanged;
InitializeSeries();
ApplyColorizer();
}
public void InitializeSeries() {
chartControl1.Series.Clear();
Series series = new Series();
series.BindToData(PointGenerator.Generate(), "X", "Y");
series.ColorDataMember = "Y";
chartControl1.Series.Add(series);
series.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;
series.Label.TextPattern = "{V:N2}";
(chartControl1.Diagram as XYDiagram).AxisX.Alignment = AxisAlignment.Zero;
}
public void ApplyColorizer() {
level = Convert.ToDouble(textBox1.Text);
Series series = chartControl1.Series[0];
StepLineSeriesView view = new StepLineSeriesView();
series.View = view;
RangeSegmentColorizer colorizer = new RangeSegmentColorizer();
colorizer.RangeStops.AddRange(new double[] { minPointValue, level, maxPointValue });
colorizer.LegendItemPattern = "{V1:0.###} - {V2:0.###}";
view.SegmentColorizer = colorizer;
}
public void OnBoundDataChanged(object sender, EventArgs e) {
Series series = chartControl1.Series[0];
minPointValue = maxPointValue = series.Points[0].Values[0];
for(int i = 1; i < series.Points.Count; i++) {
double value = series.Points[i].Values[0];
if(value < minPointValue)
minPointValue = value;
if(value > maxPointValue)
maxPointValue = value;
}
}
private void button1_Click(object sender, EventArgs e) {
InitializeSeries();
ApplyColorizer();
}
}
class PointGenerator {
public static List<SimpleDataPoint> Generate() {
Random rnd = new Random(DateTime.Now.Millisecond*2);
List<SimpleDataPoint> list = new List<SimpleDataPoint>();
for(int i = 0; i < 10; i++) {
int x = i;
double y = (rnd.NextDouble() - 0.5) * 2;
list.Add(new SimpleDataPoint(x, y));
}
return list;
}
}
public class SimpleDataPoint {
public double X { get; private set; }
public double Y { get; private set; }
public SimpleDataPoint(double arg, double val) {
X = arg;
Y = val;
}
}
}