项目作者: Indemos

项目描述 :
Generic real-time charts for WPF apps with built-in pan and zoom support. Financial and stock-trading charts.
高级语言: C#
项目地址: git://github.com/Indemos/Canvas.git
创建时间: 2021-01-13T06:09:47Z
项目社区:https://github.com/Indemos/Canvas

开源协议:MIT License

下载


Canvas - Financial Charts

The fastest charting web control targeting primarily Blazor, both Server Side and Web Assembly, and to some extent ASP.NET MVC.
This charting library was designed for Web, but it can also be used in Desktop apps via Web View.

The main purpose of this library is to be used as a real-time charting tool for financial applications that require frequent updates, e.g. backtesters for trading strategies.

Here is the most comprehensive guide dedicated to charting in .NET that I have seen so far.
Nevertheless, trying various options from that guide I wasn’t able to find anything fast and flexible enough for my needs, so created my own.

  • Samples are here
  • Example of usage in real life is a trading terminal here

Status

  1. Install-Package Canvas.Views.Web

GitHub Workflow Status (with event)
GitHub
GitHub

Implementations

Currently available controls.

  • Engine - base control exposing drawing context for various frameworks, like GDI or SkiaSharp
  • CanvasEngine - a wrapper around SkiaSharp and Open GL

To add different view types, e.g. GDI+, Direct 2D, Win UI, Open GL, implement IEngine interface.

Chart Types

At the moment, the library supports the following chart types.

  • Line - line
  • Bar - polygon
  • Area - polygon
  • Arrow - polygon
  • Candle - OHLC box
  • HeatMap - box

To add new chart types, e.g. Error Bars or Bubbles, implement IShape interface.

Sample

The simplest data format is a list of IShape models with a X and Y properties.

  1. <CanvasView @ref="View"></CanvasView>
  2. @code
  3. {
  4. public CanvasView View { get; set; }
  5. protected override async Task OnAfterRenderAsync(bool setup)
  6. {
  7. if (setup)
  8. {
  9. var generator = new Random();
  10. var points = Enumerable.Range(1, 1000).Select(i => new BarShape
  11. {
  12. X = i,
  13. Y = generator.Next(-5000, 5000)
  14. }).ToList();
  15. var composer = new Composer
  16. {
  17. Name = "Demo",
  18. Items = points
  19. };
  20. await View.Create<CanvasEngine>(engine => composer);
  21. composer.Update();
  22. }
  23. await base.OnAfterRenderAsync(setup);
  24. }
  25. }

By default, the axis X is used as an index that picks data points from the source list and axis Y is a value that represents the actual value of each data point on the vertical scale.

Preview

Synchronization

To simplify synchronization, you can use IGroupShape model instead of simple IShape.
This model allows grouping series for each chart by single timestamp, so you could display candles, lines, and other series on the same chart.

  1. Item = new
  2. {
  3. Groups = new GroupShape
  4. {
  5. ["Price Area"] = new Dictionary<string, GroupShape>
  6. {
  7. Groups = new GroupShape
  8. {
  9. ["Price Series"] = new CandleShape(),
  10. ["Arrow Series"] = new ArrowShape()
  11. }
  12. },
  13. ["Indicator Area"] = new Dictionary<string, GroupShape>
  14. {
  15. Groups = new GroupShape
  16. {
  17. ["Bar Series"] = new BarShape()
  18. }
  19. }
  20. }
  21. }

Pan and Zoom

The chart is data-centric, thus in order to scale the chart you need to change the data source.
By default, the chart displays last 100 data points, as defined in IndexCount property.

  1. MinIndex = Items.Count - IndexCount
  2. MaxIndex = Items.Count

To pan the chart to the left, subtract arbitrary value from both MinIndex and MaxIndex.

  1. MinIndex -= 1
  2. MaxIndex -= 1

To pan the chart to the right, do the opposite.

  1. MinIndex += 1
  2. MaxIndex += 1

To zoom in, increase MinIndex and decrease MaxIndex to decrease number of visible points.

  1. MinIndex += 1
  2. MaxIndex -= 1

To zoom out, do the opposite.

  1. MinIndex -= 1
  2. MaxIndex += 1

Roadmap

To increase performance, the chart is split into pieces and each piece is using its own thread, so UI is never blocked even while rendering 100K samples.
To increase performance even further, downsampling could be implemented, e.g. when number of points is greater that width of the screen in pixels, because all points wouldn’t fit on the screen anyway.