Showing Trend Marks in C1Chart
The following example code shows how to use the trend indicators in the chart with triangles used as arrows:
chart.ChartType = ChartType.LineSymbols
Dim values As Double() = New Double() {1, 4, 3, 2, 4, 7}
Dim ds As New DataSeries()
ds.Loaded += Function(sender, args) Do
Dim rp As RPolygon = TryCast(sender, RPolygon)
If rp IsNot Nothing Then
Dim pi As Integer = rp.DataPoint.PointIndex
If pi > 0 Then
'rotate triangle and change its color
If values(pi) > values(pi - 1) Then
rp.RenderTransform = New RotateTransform()
rp.RenderTransformOrigin = New Point(0.5, 0.5)
rp.Fill = New SolidColorBrush(Colors.Green)
ElseIf values(pi) < values(pi - 1) Then
rp.RenderTransform = New RotateTransform()
rp.RenderTransformOrigin = New Point(0.5, 0.5)
rp.Fill = New SolidColorBrush(Colors.Red)
End If
End If
End If
End Function
chart.Data.Children.Add(ds)
•C#
chart.ChartType = ChartType.LineSymbols;
double[] values = new double[] { 1, 4, 3, 2, 4, 7 };
DataSeries ds = new DataSeries()
{
ConnectionStroke = new SolidColorBrush(Colors.DarkGray),
ConnectionStrokeThickness=1,
Symbol = new RPolygon(),
SymbolSize = new Size(18,12),
ValuesSource = values
};
ds.Loaded += (sender, args) =>
{
RPolygon rp = sender as RPolygon;
if (rp != null)
{
int pi = rp.DataPoint.PointIndex;
if (pi > 0)
{
//rotate triangle and change its color
if ( values[pi] > values[pi-1] )
{
rp.RenderTransform = new RotateTransform() { Angle = -90 };
rp.RenderTransformOrigin = new Point(0.5, 0.5);
rp.Fill = new SolidColorBrush(Colors.Green);
}
else if (values[pi] < values[pi - 1])
{
rp.RenderTransform = new RotateTransform() { Angle = 90 };
rp.RenderTransformOrigin = new Point(0.5, 0.5);
rp.Fill = new SolidColorBrush(Colors.Red);
}
}
}
};
chart.Data.Children.Add(ds);
Using Automatic Series Generation
The following feature is useful in various MVVM scenarios and allows you to automatically generate series based on a provided template.
Use the following XAML for a chart declaration:
<c1:C1Chart
Name="chart">
<c1:C1Chart.DataContext>
<!-- sample data
-->
<local:ChartData
/>
</c1:C1Chart.DataContext>
<c1:C1Chart.Data>
<c1:ChartData
SeriesItemsSource="{Binding SeriesDataCollection}"
>
<c1:ChartData.SeriesItemTemplate>
<!-- template for series
-->
<DataTemplate>
<c1:DataSeries Label="{Binding
Name}"
ValuesSource="{Binding Values}"
/>
</DataTemplate>
</c1:ChartData.SeriesItemTemplate>
</c1:ChartData>
</c1:C1Chart.Data>
<c1:C1ChartLegend
/>
</c1:C1Chart>
Then use the following code for a sample data model:
public class
ChartData
{
public static int NUM_SERIES = 5;
public
ObservableCollection<SeriesData>
SeriesDataCollection
{
get
{
if (_seriesDataCollection ==
null)
{
_seriesDataCollection =
new
ObservableCollection<SeriesData>();
for (int i = 0; i < NUM_SERIES;
i++)
_seriesDataCollection.Add(new SeriesData("Series " + i));
}
return
_seriesDataCollection;
}
}
private
ObservableCollection<SeriesData> _seriesDataCollection;
}
public
class SeriesData
{
static int NUM_POINTS = 10;
static Random rnd = new
Random();
public SeriesData(string name)
{
Name =
name;
Values = new double[NUM_POINTS];
for (int i = 0; i <
NUM_POINTS; i++)
Values[i] = rnd.Next(0, 100);
}
public string Name
{ get; set; }
public double[] Values { get; set; }
}