1

I'm trying change the line thickness in a serie dinamically created, I need turn the line more thick.

Below, follow the code to bind the created serie on chart component. It works fine, but I tryed adapt this in this code and I had no sucess.

Please help, thanks.

Style style = new Style(typeof(LineDataPoint));
style.Setters.Add(new Setter(LineDataPoint.OpacityProperty, (double)(0.0)));
style.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, dadosSerie.ColorSerie));

LineSeries lineSerie = new LineSeries()
{
    Title = dadosSerie.SerieTitle,
    IndependentValueBinding = new Binding("Key"),
    DependentValueBinding = new Binding("Value"),
    DependentRangeAxis = dadosSerie.EixoY,
    DataPointStyle = style,
    ItemsSource = dadosSerie.DataSerie,
};

chtGraficos.Series.Add(lineSerie);
Community
  • 1
  • 1
Jeferson Preti
  • 57
  • 2
  • 12

1 Answers1

1

Have you tried adding a Style for the serie's Polyline instead? It seams the style for the LineDataPoint is actually for every point on the serie.

Here is a working sample of a chart fully created on code-behind. You just have to create a window named MainWindow and add a reference on the project to System.Windows.Controls.DataVisualization.Toolkit:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var valueList = new Dictionary<string, int>();
        valueList.Add("Developer", 60);
        valueList.Add("Misc", 20);
        valueList.Add("Project Manager", 40);

        var style = new Style(typeof(Polyline));
        style.Setters.Add(new Setter(Polyline.StrokeThicknessProperty, 10d));

        var series = new LineSeries
        {
            PolylineStyle = style,
            ItemsSource = valueList,
            DependentValuePath = "Value",
            IndependentValuePath = "Key",
        };

        var lineChart = new Chart { Height = 254 };
        lineChart.Series.Add(series);

        var mainGrid = new Grid();
        mainGrid.Children.Add(lineChart);

        this.Content = mainGrid;
    }
}
Gabriel Rainha
  • 1,713
  • 1
  • 21
  • 34
  • Thank you for answer Gabriel. Only by set the PolyLineStyle, the line turned more thin, but I can't change this thickness by StrokeThicknessProperty: the thickness always keep the same (I change the value from 10d to 5000d). I tried set the WithProperty, but the thickness don't changed, too. Thank you again. – Jeferson Preti May 16 '15 at 14:03
  • Hi Gabriel. I tryed adapt your code in mine, but I had problems with chart.DataContext = valueList, because I need more than one serie in the chart, where some series I need turn the thickness more thick, and another, more thin. Because this, I need add the dataList as Series. So, in my code, I only add: { ... PolylineStyle = styleThickness, DependentValuePath = "Value", IndependentValuePath = "Key", } without sucess. Thank you for answer. – Jeferson Preti May 19 '15 at 13:56
  • The data I use as datasource doesn't make any difference on the style. I just edited the sample to show you that, by assigning the collection directly as the ItemsSource of the serie, exactly as you did. Perhaps I'm not understanding your problem: Does your chart correctly display the data and only the line thickness does not comply, or is it that no data is displayed? – Gabriel Rainha May 19 '15 at 16:36
  • Hi Gabriel, the data are displayed perfectly, but I can't change the thickness of lineseries. My code is exactly like your now, even so, I can't change this thickness. In this time, I set the Polyline.StrokeThicknessProperty when I want more thin line, and I don't set this property when I want more thick line. It works, but I would like more thick lines. Thank you for patience and attention! – Jeferson Preti May 19 '15 at 21:23
  • Well, the only thing that I can think of now is that you are adding other styles after that one that are overriding the definition set there. Check out for any code-behind (or XAML style-definition) that are related to Polyline. Do a Find All References on Polyline for the .cs files and a Find in Files searching for text TargetType="Polyline" and TargetType="{x:Type Polyline}" for the .xaml files . – Gabriel Rainha May 19 '15 at 21:30
  • Just to prove you that the code I posted works, create a new WPF Application project, paste that on the MainWindow.xaml.cs code-behind, add a reference to the WPF Data Visualization Toolkit and run it. The problem must lie somewhere else not seen on your sample code. – Gabriel Rainha May 19 '15 at 21:33
  • Yes, I believe in this too. This behavior can happens by some illegal command. I'll try detect this. By the way, thank you for the help Gabriel. Great hug. – Jeferson Preti May 20 '15 at 11:51
  • Gabriel, if you can help me one last time, I can't detect what is wrong. My chart config in xaml is very simple: You had sucess in case of chart created by xaml (only series created in code behind?) – Jeferson Preti May 20 '15 at 12:48
  • Yes, I did. My original implementation was actually like you said, but I changed it to be 100% on code-behind. It worked on both cases. – Gabriel Rainha May 20 '15 at 12:53