Code Monkey home page Code Monkey logo

canvas's Introduction

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

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, a mix of a line and a 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.

<CanvasView @ref="View"></CanvasView>

@code
{
  public CanvasView View { get; set; }

  protected override async Task OnAfterRenderAsync(bool setup)
  {
    if (setup)
    {
      var generator = new Random();
      var points = Enumerable.Range(1, 1000).Select(i => new BarShape 
      { 
        X = i, 
        Y = generator.Next(-5000, 5000) 
     
     }).ToList();
      
      var composer = new Composer
      {
        Name = "Demo",
        Items = points
      };

      await View.Create<CanvasEngine>(engine => composer);

      composer.Update();
    }

    await base.OnAfterRenderAsync(setup);
  }
}

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.

Item = new 
{
  Groups = new GroupShape
  {
    ["Price Area"] = new Dictionary<string, GroupShape>
    {
      Groups = new GroupShape
      {
        ["Price Series"] = new CandleShape(),
        ["Arrow Series"] = new ArrowShape()
      }
    },
    ["Indicator Area"] = new Dictionary<string, GroupShape>
    {
      Groups = new GroupShape
      { 
        ["Bar Series"] = new BarShape() 
      }
    }
  }
}

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.

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

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

MinIndex -= 1
MaxIndex -= 1

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

MinIndex += 1
MaxIndex += 1

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

MinIndex += 1
MaxIndex -= 1

To zoom out, do the opposite.

MinIndex -= 1
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.

canvas's People

Contributors

artemiusgreat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

canvas's Issues

Question: Managing many text nodes, updating efficiently

Hi all

I am looking at implementing a footprint chart in Canvas (example attached).

These charts rely on updating several nodes as they are calculated as each price level update event comes in. These events come in very quickly, though can be batched.

image

What would be the best approach to this given that one update can mean that many text nodes need to be updated, as well as possibly re-rendering boxes to update the fill color.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.