SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

1
0

I managed to spearate the legend from the SciChartSurface, however the way I did it is very hackish. I couldn’t find any documentation on how it is supposed to be done correctly.

The way I did it (Kotlin code):

val legend = SciChartLegend(context)
legendModifier = LegendModifier(legend)
legendModifier.setOrientation(Orientation.VERTICAL)
legendModifier.setLegendPosition(Gravity.START or Gravity.TOP, 0)
legendModifier.setSourceMode(SourceMode.AllVisibleSeries)
legendModifier.setShowSeriesMarkers(true)
legendModifier.setShowCheckboxes(true)


val modifierGroup = chartBuilder.newModifierGroup().build()
modifierGroup.childModifiers.add(legendModifier)
chartSurface.chartModifiers.add(modifierGroup)

if(legend.parent != null)
{
    (legend.parent as ViewGroup).removeView(legend)
}

val linearLayout = LinearLayout(context)
linearLayout.orientation = LinearLayout.VERTICAL
linearLayout.addView(legend)
linearLayout.addView(chartSurface)

I was surprised that passing the legend to LegendModifier constructor also adds it to it’s layout. So I had to manually remove it and add it to my LinearLayout. It does work, but as I said, it is a hackish way of doing it. My question is, how is it done correctly?

Version
v2.0.0.1884
  • You must to post comments
0
0

Hi Miha,

Well unfortunately it is a default behavior of LegendModifier and it will be hard to change because:

  1. When it is added into modifier collection it becomes part of SciChartSurface so it has access only to the chart and its subviews and it can place it only there.
  2. LegendModifier has properties which are responsible for legend placement inside parent ViewGroup and they probably won’t work properly with other ViewGroup container.

The purpose of constructor which accepts SciChartLegend is to allow customization of legend appearance (e.g. you can configure some legend properties before passing it to the modifier or you can subclass it if you want )


Alternative to using LegendModifier is creating and managing content of SciChartLegend on your own but it will require writing of code for creating and placing SciChartLegend in your app, creating LegendItemsAdapter and filling it with data from chart, creating listeners for different chart events (e.g. renderable series was added/removed, series name, color, visibility changed etc) and updating data when something in chart changes. I wouldn’t say that it would be easy to do this.


Anyway I’ve logged your request into our bugtracker and we’ll try to improve LegendModifier API in one of our future releases.

Best regards,
Yura

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.