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

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy. We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

Please note: SciChart team will only answer questions from customers with active support subscriptions. Expired support questions will be ignored. If your support status shows incorrectly, contact us and we will be glad to help.

0
0

I have some complex data & am showing each dataset as a different colour, but I would like to be able to customise the legends.

I am interested in having 2 legends, one with each data set, showing the colour, and preferably, limited to a set number with the option to scroll, and the other showing that solid lines are the left Y axis, and the dashed lines are the right Y axis.

My question is, is there a way to apply a custom skin to the Legend & is there a way to show dashed lines instead of just a big coloured square in the legend?

Version
v2.2.1.2391
  • You must to post comments
1
0

Hi Peter,

You can customize legend item appearance this way:
1. define custom layout for legend item ( I’m using the same layout as default item and you can replace LegendPointMarker on custom View which draws anything you want )

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/legendItemMargin"
android:orientation="horizontal">

<CheckBox
    android:id="@+id/isVisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="@dimen/legendTextSize"
    android:layout_marginLeft="@dimen/legendItemCheckBoxNegativeMargin"
    android:layout_marginTop="@dimen/legendItemCheckBoxNegativeMargin"
    android:layout_marginRight="@dimen/legendItemCheckBoxNegativeMargin"
    android:layout_marginBottom="@dimen/legendItemCheckBoxNegativeMargin" />

<com.scichart.charting.visuals.legend.LegendPointMarker
    android:id="@+id/pointMarker"
    android:layout_width="@dimen/legendItemPointMarkerWidth"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="@dimen/legendItemPointMarkerMargin"
    android:layout_marginTop="@dimen/legendItemPointMarkerMargin" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|start"
    android:layout_marginEnd="@dimen/legendItemSeriesNameRightMargin"
    android:layout_marginStart="@dimen/legendItemSeriesNameLeftMargin"
    android:textSize="@dimen/legendTextSize"
    android:textStyle="bold" />

</LinearLayout>
  1. Then you need to create a custom class which implements ILegendItem ( wrapper for legend View ) and ILegendItemFactory ( factory which can create items ) interfaces:

    class CustomLegendItemFactory implements ILegendItemsFactory {
    
    @Override
    public ILegendItem createLegendItem(ViewGroup parent) {
        return new CustomLegendItem(LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_legend_item, null));
    }
    }
    
    class CustomLegendItem extends LegendItemBase {
    protected final TextView name;
    protected final CheckBox checkBox;
    protected final LegendPointMarker pointMarker;
    
    public CustomLegendItem(@NonNull View itemView) {
        super(itemView);
    
        name = (TextView) itemView.findViewById(com.scichart.charting.R.id.name);
        checkBox = (CheckBox) itemView.findViewById(com.scichart.charting.R.id.isVisible);
        pointMarker = (LegendPointMarker) itemView.findViewById(com.scichart.charting.R.id.pointMarker);
    }
    
    @Override
    public void bindSeriesInfo(Object source, SciChartLegend legend) {
        final SeriesInfo seriesInfo = (SeriesInfo) source;
    
        name.setText(seriesInfo.seriesName);
        checkBox.setChecked(seriesInfo.isVisible());
        checkBox.setVisibility(legend.getShowCheckboxes() ? View.VISIBLE : View.GONE);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IRenderableSeries renderableSeries = seriesInfo.renderableSeries;
                renderableSeries.setIsVisible(((CheckBox) view).isChecked());
                renderableSeries.invalidateElement();
            }
        });
        pointMarker.setColor(seriesInfo.seriesColor);
        pointMarker.setVisibility(legend.getShowSeriesMarkers() ? View.VISIBLE : View.GONE);
    
        ThemeManager.applyTheme(this, legend.getTheme(), legend.getContext());
    }
    
    @Override
    public void applyThemeProvider(IThemeProvider themeProvider) {
        final FontStyle legendLabelTextStyle = themeProvider.getDefaultLabelTextStyle();
        legendLabelTextStyle.initTextView(name);
    }
    }
    

    You’ll need to customize bindSeriesInfo() here and instead of pointMarker get your own View and set color to draw.

  2. And finally use factory to create LegendModifier and add it into chart’s modifier collection:

                legendModifier = new LegendModifier(new SciChartLegend(getActivity()), new SeriesInfoLegendAdapter(new CustomLegendItemFactory()), true);
    
            ModifierGroup modifierGroup = sciChartBuilder.newModifierGroup()
                    .withModifier(legendModifier)
                    .build();
    

Hope this will help you!

Best regards,
Yura

  • You must to post comments
0
0

That’s great! Thanks for the help!

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.