Android Open Source - charts-custom-tickmarks-and-baseline-android Custom Tick Marks And Baseline Activity






From Project

Back to project page charts-custom-tickmarks-and-baseline-android.

License

The source code is released under:

Apache License

If you think the Android project charts-custom-tickmarks-and-baseline-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Scott Logic//www.  j a va 2 s.co m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shinobicontrols.charts.demos.customtickmarksandbaseline;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.shinobicontrols.charts.Annotation;
import com.shinobicontrols.charts.AnnotationsManager;
import com.shinobicontrols.charts.Axis;
import com.shinobicontrols.charts.CategoryAxis;
import com.shinobicontrols.charts.ChartFragment;
import com.shinobicontrols.charts.ColumnSeries;
import com.shinobicontrols.charts.DataAdapter;
import com.shinobicontrols.charts.DataPoint;
import com.shinobicontrols.charts.LineSeries;
import com.shinobicontrols.charts.NumberAxis;
import com.shinobicontrols.charts.NumberRange;
import com.shinobicontrols.charts.SeriesStyle.FillStyle;
import com.shinobicontrols.charts.ShinobiChart;
import com.shinobicontrols.charts.SimpleDataAdapter;
import com.shinobicontrols.charts.TickMark;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * This simple app demonstrates the following features:
 * 
 * - setting custom baselines on line and column series
 * 
 * - providing a set of major tick values for an axis to display
 * 
 * - individual customization of tick marks
 * 
 * - add range padding to an axis
 * 
 * - adding annotations to your chart
 */
public class CustomTickMarksAndBaselineActivity extends Activity implements
        ShinobiChart.OnTickMarkUpdateListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_baseline);

        // Only set the chart up the first time the Activity is created
        if (savedInstanceState == null) {

            // Get the a reference to the ShinobiChart from the ChartFragment
            ChartFragment chartFragment =
                    (ChartFragment) getFragmentManager().findFragmentById(R.id.chartfragment);

            ShinobiChart shinobiChart = chartFragment.getShinobiChart();

            // TODO: replace <license_key_here> with your trial license key
            shinobiChart.setLicenseKey("<license_key_here>");

            shinobiChart.setTitle("Monthly Sales Figures");

            // Create the axes and given them to the chart
            CategoryAxis xAxis = createXAxis();
            NumberAxis revenueYAxis = createRevenueYAxis();
            NumberAxis unitsYAxis = createUnitsYAxis();

            shinobiChart.setXAxis(xAxis);
            shinobiChart.setYAxis(revenueYAxis);
            shinobiChart.addYAxis(unitsYAxis);

            // Create our series (with data) and give them to the chart
            LineSeries revenueLineSeries = createRevenueLineSeries();
            revenueLineSeries.setDataAdapter(createRevenueData());

            ColumnSeries unitsColumnSeries = createUnitsColumnSeries();
            unitsColumnSeries.setDataAdapter(createUnitsData());

            shinobiChart.addSeries(revenueLineSeries);
            shinobiChart.addSeries(unitsColumnSeries, xAxis, unitsYAxis);

            // Add some annotations to the chart
            addAnnotations(shinobiChart);

            // Set this Activity as the chart's OnTickMarkUpdateListener
            shinobiChart.setOnTickMarkUpdateListener(this);
        }
    }

    private CategoryAxis createXAxis() {
        CategoryAxis axis = new CategoryAxis();

        axis.setRangePaddingHigh(0.5);
        axis.setRangePaddingLow(0.5);
        axis.setExpectedLongestLabel("Forecast\nForecast");

        return axis;
    }

    private NumberAxis createRevenueYAxis() {
        NumberAxis axis = new NumberAxis(new NumberRange(0.0, 115.0));

        axis.setTitle("Revenue");
        axis.setMajorTickFrequency(25.0);
        axis.setLabelFormat(new DecimalFormat("$#K"));

        return axis;
    }

    private NumberAxis createUnitsYAxis() {
        NumberAxis axis = new NumberAxis(new NumberRange(0.0, 40.0));

        axis.setTitle("Units");
        axis.setPosition(Axis.Position.REVERSE);
        axis.setLabelFormat(new DecimalFormat("#K"));

        List<Double> tickMarkValues = new ArrayList<Double>();
        tickMarkValues.add(Double.valueOf(0.0));
        tickMarkValues.add(Double.valueOf(5.0));
        tickMarkValues.add(Double.valueOf(10.0));
        axis.setMajorTickMarkValues(tickMarkValues);

        return axis;
    }

    private LineSeries createRevenueLineSeries() {
        LineSeries series = new LineSeries();

        series.setBaseline(75);

        series.getStyle().setLineWidth(2.0f);
        series.getStyle().setLineColor(getResources().getColor(R.color.my_green));
        series.getStyle().setLineColorBelowBaseline(getResources().getColor(R.color.my_red));

        series.getStyle().getPointStyle().setPointsShown(true);
        series.getStyle().getPointStyle().setInnerRadius(0.0f);
        series.getStyle().getPointStyle().setColor(getResources().getColor(R.color.my_green));
        series.getStyle().getPointStyle()
                .setColorBelowBaseline(getResources().getColor(R.color.my_red));

        return series;
    }

    private DataAdapter<?, ?> createRevenueData() {
        DataAdapter<String, Integer> data = new SimpleDataAdapter<String, Integer>();

        data.add(new DataPoint<String, Integer>("Jan", 52));
        data.add(new DataPoint<String, Integer>("Feb", 79));
        data.add(new DataPoint<String, Integer>("Mar", 102));
        data.add(new DataPoint<String, Integer>("Apr", 63));
        data.add(new DataPoint<String, Integer>("May", 79));
        data.add(new DataPoint<String, Integer>("Jun", 87));

        return data;
    }

    private ColumnSeries createUnitsColumnSeries() {
        ColumnSeries series = new ColumnSeries();

        series.setBaseline(7.5);

        series.getStyle().setFillStyle(FillStyle.FLAT);
        series.getStyle().setLineShown(false);
        series.getStyle().setAreaColor(getResources().getColor(R.color.my_green));
        series.getStyle().setAreaColorBelowBaseline(getResources().getColor(R.color.my_red));

        return series;
    }

    private DataAdapter<?, ?> createUnitsData() {
        DataAdapter<String, Double> data = new SimpleDataAdapter<String, Double>();

        data.add(new DataPoint<String, Double>("Jan", 4.9));
        data.add(new DataPoint<String, Double>("Feb", 8.0));
        data.add(new DataPoint<String, Double>("Mar", 11.0));
        data.add(new DataPoint<String, Double>("Apr", 3.0));
        data.add(new DataPoint<String, Double>("May", 7.9));
        data.add(new DataPoint<String, Double>("Jun", 10.0));

        return data;
    }

    private void addAnnotations(ShinobiChart shinobiChart) {
        AnnotationsManager manager = shinobiChart.getAnnotationsManager();
        Axis<?, ?> xAxis = shinobiChart.getXAxis();
        Axis<?, ?> revenueYAxis = shinobiChart.getYAxis();
        Axis<?, ?> unitsYAxis = shinobiChart.getAllYAxes().get(1);

        manager.addTextAnnotation("TARGET", 5.75, 70, xAxis, revenueYAxis);

        manager.addTextAnnotation("TARGET", 5.75, 6, xAxis, unitsYAxis);

        // We'll add the target lines as custom view Annotations - the X value
        // is irrelevant here as the annotation will stretch across the plot
        // area so we just set it to null
        Annotation revenueTargetLine = manager.addViewAnnotation(
                createDottedLineView(), null, 75, xAxis, revenueYAxis);
        revenueTargetLine.setPosition(Annotation.Position.BEHIND_DATA);

        Annotation unitsTargetLine = manager.addViewAnnotation(
                createDottedLineView(), null, 7.5, xAxis, unitsYAxis);
        unitsTargetLine.setPosition(Annotation.Position.BEHIND_DATA);
    }

    private View createDottedLineView() {
        View view = new View(this);

        // A bug in the Android framework prevents the dotted line being drawn
        // unless this is set!
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        // Use our dotted shape drawable as the background and set it depending
        // on Android version
        Drawable background = getResources().getDrawable(R.drawable.dotted);
        setBackgroundCompat(view, background);

        // MATCH_PARENT on the width so the annotation stretches across the plot
        // area
        view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 2));

        return view;
    }

    @SuppressLint("NewApi")
    private void setBackgroundCompat(View view, Drawable background) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(background);
        }
        else {
            view.setBackgroundDrawable(background);
        }
    }

    @Override
    public void onUpdateTickMark(TickMark tickMark, Axis<?, ?> axis) {

        if (axis.getOrientation() == Axis.Orientation.HORIZONTAL) {

            if (isCurrentMonth(tickMark.getLabelText())) {
                tickMark.setLabelText("This\nMonth");
            }
            else if (isFutureMonth(tickMark.getLabelText())) {
                tickMark.setLabelText(tickMark.getLabelText() + "\nForecast");
                tickMark.getLabelPaint().setColor(getResources().getColor(R.color.my_grey));
            }
        }
    }

    private boolean isCurrentMonth(String labelText) {
        // For simplicity this has been hard-coded with strings... it's left as
        // an exercise for the reader to do this properly with Dates. ;-)
        return labelText == "Apr";
    }

    private boolean isFutureMonth(String labelText) {
        // For simplicity this has been hard-coded with strings... it's left as
        // an exercise for the reader to do this properly with Dates. ;-)
        return labelText == "May" || labelText == "Jun";
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.custom_baseline, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}




Java Source Code List

com.shinobicontrols.charts.demos.customtickmarksandbaseline.CustomTickMarksAndBaselineActivity.java