SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
SciChart WPF ships with hundreds of WPF Chart Examples which you can browse, play with, view the source-code and even export each WPF Chart Example to a stand-alone Visual Studio solution. All of this is possible with the new and improved SciChart WPF Examples Suite, which ships as part of the SciChart WPF SDK.
Demonstrates how to create a Heatmap with Text in the cells SciChart. Accepts a 2D Array of data and has a user-defined colour map which can be displayed as a legend over the chart.
Documentation Links
– Heatmap API Documentation
– RenderableSeries API
– UniformHeatmapDataSeries class
– NonUniformHeatmapDataSeries class
The C#/WPF source code for the WPF Heatmap Chart with Text example is included below (Scroll down!).
Did you know you can also view the source code from one of the following sources as well?
<UserControl x:Class="SciChart.Examples.Examples.HeatmapChartTypes.HeatmapWithText.HeatMapWithTextInCellsExampleView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ext="http://schemas.abtsoftware.co.uk/scichart/exampleExternals"
xmlns:system="clr-namespace:System;assembly=mscorlib"
d:DesignHeight="400"
d:DesignWidth="600"
mc:Ignorable="d">
<UserControl.Resources>
<s:GradientStopsToLinearGradientBrushConverter x:Key="ColorsToLinearGradientBrushConverter"/>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- The SciChartInteractionToolbar adds zoom, pan, zoom extents and rotate functionality -->
<!-- to the chart and is included for example purposes. -->
<!-- If you wish to know how to zoom and pan a chart then do a search for Zoom Pan in the Examples suite! -->
<ext:SciChartInteractionToolbar TargetSurface="{Binding Source={x:Reference Name=sciChart}}"/>
<s:SciChartSurface x:Name="sciChart" Grid.Column="1"
Padding="0"
BorderThickness="0">
<s:SciChartSurface.RenderableSeries>
<s:FastUniformHeatmapRenderableSeries x:Name="heatmapSeries"
DrawTextInCell="True"
Opacity="0.9">
<s:FastUniformHeatmapRenderableSeries.ColorMap>
<s:HeatmapColorPalette Minimum="0" Maximum="100">
<GradientStop Color="Blue" Offset="0"/>
<GradientStop Color="White" Offset="0.3"/>
<GradientStop Color="Green" Offset="0.5"/>
<GradientStop Color="Yellow" Offset="0.7"/>
<GradientStop Color="Red" Offset="1"/>
</s:HeatmapColorPalette>
</s:FastUniformHeatmapRenderableSeries.ColorMap>
</s:FastUniformHeatmapRenderableSeries>
</s:SciChartSurface.RenderableSeries>
<s:SciChartSurface.XAxis>
<s:NumericAxis x:Name="xAxis"
FlipCoordinates="True"
GrowBy="0.1,0.1"
ScientificNotation="None"
DrawMajorBands="True" />
</s:SciChartSurface.XAxis>
<s:SciChartSurface.YAxis>
<s:NumericAxis x:Name="yAxis"
FlipCoordinates="True"
GrowBy="0.2,0.4"
ScientificNotation="None"
DrawMajorBands="True" />
</s:SciChartSurface.YAxis>
<s:SciChartSurface.ChartModifier>
<s:ModifierGroup>
<s:RubberBandXyZoomModifier ExecuteOn="MouseLeftButton" />
<s:ZoomExtentsModifier ExecuteOn="MouseDoubleClick" />
<s:XAxisDragModifier />
<s:YAxisDragModifier />
</s:ModifierGroup>
</s:SciChartSurface.ChartModifier>
</s:SciChartSurface>
<s:HeatmapColorMap Margin="30"
Grid.Column="1"
Background="{Binding Source={x:Reference Name=sciChart}, Path=Background}"
Foreground="{Binding Source={x:Reference Name=sciChart}, Path=Foreground}"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom"
DataContext="{Binding Source={x:Reference Name=heatmapSeries}, Mode=OneWay}"
ColorMap="{Binding ColorMap.GradientStops, Converter={StaticResource ColorsToLinearGradientBrushConverter}}"
Minimum="{Binding ColorMap.Minimum}"
Maximum="{Binding ColorMap.Maximum}"
TextFormatting="n0"
Opacity="0.9" />
</Grid>
</UserControl>
// *************************************************************************************
// SCICHART® Copyright SciChart Ltd. 2011-2021. All rights reserved.
//
// Web: http://www.scichart.com
// Support: support@scichart.com
// Sales: sales@scichart.com
//
// HeatMapWithTextInCellsExampleView.xaml.cs is part of the SCICHART® Examples. Permission is hereby granted
// to modify, create derivative works, distribute and publish any part of this source
// code whether for commercial, private or personal use.
//
// The SCICHART® examples are distributed in the hope that they will be useful, but
// without any warranty. It is provided "AS IS" without warranty of any kind, either
// expressed or implied.
// *************************************************************************************
using System;
using System.Globalization;
using System.Windows.Controls;
using SciChart.Charting.Model.DataSeries;
using SciChart.Charting.Visuals.Axes.LabelProviders;
using SciChart.Charting.Model.DataSeries.Heatmap2DArrayDataSeries;
using SciChart.Core.Extensions;
namespace SciChart.Examples.Examples.HeatmapChartTypes.HeatmapWithText
{
/// <summary>
/// Interaction logic for HeatMapWithTextInCellsExampleView.xaml
/// </summary>
public partial class HeatMapWithTextInCellsExampleView : UserControl
{
private class YAxisLabelProvider: LabelProviderBase
{
public override string FormatLabel(IComparable dataValue)
{
try
{
switch (Convert.ToInt32(dataValue))
{
case 0: return "Mon";
case 1: return "Tue";
case 2: return "Wed";
case 3: return "Thu";
case 4: return "Fri";
case 5: return "Sat";
case 6: return "Sun";
default: return string.Empty;
}
}
catch
{
return string.Empty;
}
}
public override string FormatCursorLabel(IComparable dataValue)
{
return FormatLabel(dataValue);
}
}
private class XAxisLabelProvider : LabelProviderBase
{
public override string FormatLabel(IComparable dataValue)
{
var h = (int) Math.Ceiling(dataValue.ToDouble());
var dt = new DateTime(2000, 1, 1, 1, 0, 0);
try
{
dt = dt.AddHours(h);
}
catch (ArgumentOutOfRangeException)
{
dt = h < 0 ? DateTime.MinValue : DateTime.MaxValue;
}
return dt.ToString("hh:mm tt", new CultureInfo("en-US"));
}
public override string FormatCursorLabel(IComparable dataValue)
{
return FormatLabel(dataValue);
}
}
private IDataSeries CreateSeries()
{
const int w = 24;
const int h = 7;
var rnd = new Random(0);
var data = new double[h, w];
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
data[y, x] = Math.Pow(rnd.NextDouble(), 0.15) * x / (w - 1) * y / (h - 1) * 100;
}
}
return new UniformHeatmapDataSeries<int, int, double>(data, 0, 1, 0, 1);
}
public HeatMapWithTextInCellsExampleView()
{
InitializeComponent();
yAxis.LabelProvider = new YAxisLabelProvider();
xAxis.LabelProvider = new XAxisLabelProvider();
heatmapSeries.DataSeries = CreateSeries();
}
}
}