beto-rodriguez / LiveCharts2

Simple, flexible, interactive & powerful charts, maps and gauges for .Net, LiveCharts2 can now practically run everywhere Maui, Uno Platform, Blazor-wasm, WPF, WinForms, Xamarin, Avalonia, WinUI, UWP.
https://livecharts.dev
MIT License
4.34k stars 566 forks source link

Question: WPF Binding of e.g. LineSeries #303

Closed avsteele closed 2 years ago

avsteele commented 2 years ago

Hi Beto. I'm a LiveCharts Geared user and looking at moving over to the new version but can't figure out how to bind & style my line series in the View.

I the previous LiveCharts/Geared the line series were DependencyObjects so it was straightforward to use <Style> in the View/XAML and provide the data via a binding via Values={Binding ...}.

In LIveCharts2 the only dependency property i see is Series on the chart. Which would appear to mean the data and series styling would need to be together on the ViewModel. Is this correct? The samples are done this way, with both the styling and data together.

beto-rodriguez commented 2 years ago

Hi @avsteele

V2 takes a different approach, it is focused to run everywhere, you could share the same view model in a desktop application (WPF, Avalonia, WinForms), mobile (Xamarin, MAUI) and the web (Blazor).

Sadly yes, as you noticed series are no longer DependencyObjects , but that does not means that we can not build a wrapper for WPF so they work as dependency objects again, but if we add this, then we should also add it for all the platforms (UWP, WinUI, Xamarin, Maui) and it will take some time to develop, it is not in my plans to implement this feature in the first stable release of v2.

But the library has a solution to style charts quickly, v2 has themes, the open source version includes the light and dark themes, but there are a lot of themes, the API is simple:

LiveCharts.Configure(settings => settings .AddLightTheme());

// or the dark theme 
LiveCharts.Configure(settings => settings .AddDarkTheme()); 

// or the dark theme but with custom rules
LiveCharts.Configure(
      settings => settings .AddDarkTheme( theme =>
              {
                  // you can add additional rules to the current theme
                  theme.Style
                      .HasRuleForLineSeries(lineSeries =>
                      {
                          // this method will be called in the constructor of a line series instance

                          lineSeries.LineSmoothness = 0.65;
                          // ...
                          // add more custom styles here ...
                      }).HasRuleForBarSeries(barSeries =>
                      {
                          // this method will be called in the constructor of a column series instance
                          // ...
                      });
              }));

It is not documented properly yet, but hopefully this solution will be enough, what do you think?

avsteele commented 2 years ago

Thank much for your reply. Themes definitely help, but if I understand correctly, they can only bring you part way, as they are global and set once for each class?

For example, in plots containing multiple series you need to be able to, say, set the stroke, color or point geometry for each series. Can themes help here?

I'll think about the use of writing some wrappers for my own application in the meantime if this is not on the roadmap.

beto-rodriguez commented 2 years ago

Yes you got it right, at this point (beta-80) they only work globally, but it is in the road map to have a solution to apply styles to a specific chart or series.