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?
- MIha Rozina asked 7 years ago
- You must login to post comments
Hi Miha,
Well unfortunately it is a default behavior of LegendModifier and it will be hard to change because:
- 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.
- 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.
UPDATED
We’ve changed behavior of constructor which accepts SciChartLegend instance in v2.1. Now when you’re using this constructor it’s expected that legend will be placed somewhere in layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.scichart.charting.visuals.SciChartSurface
android:id="@+id/chart"
android:layout_width="fill_parent"
android:layout_height="0dp" android:layout_weight="1" />
<com.scichart.charting.visuals.legend.SciChartLegend
android:id="@+id/legend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"/>
</LinearLayout>
and then legend instance will be passed into LegendModifier:
final LegendModifier legendModifier = new LegendModifier(legend);
In this case LegendModifier’s properties which are responsible for placing legend within chart won’t work.
To support previous case when you just need to customize SciChartLegend appearance you can use new constructor which accepts useAutoPlacement flag:
final LegendModifier legendModifier = new LegendModifier(legend, new SeriesInfoLegendAdapter(), true);
Best regards,
Yura
- Yura Khariton answered 7 years ago
- last edited 6 years ago
-
Expecting fix above issue from scichart very soon !
-
Actually it was fixed in v2.1 during work on pie/donut charts ( we needed to place legend outside chart in demo examples for these chart types ). Also I updated my answer. Hope this will help you!
- You must login to post comments
Please login first to submit.