com.github.mikephil.charting.components.Legend Java Examples

The following examples show how to use com.github.mikephil.charting.components.Legend. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KlineFragment.java    From shinny-futures-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * date: 2019/7/31
 * author: chenli
 * description: 刷新均线图标值
 */
private void refreshTopChartLegend(int index) {
    List<LegendEntry> legendEntries = new ArrayList<>();
    for (int i = 0; i < mas.size(); i++) {
        int para = mas.get(i);
        Entry entry = mLineData.getDataSetByIndex(i).getEntryForXValue(index, 0.0f);
        String data = ":";
        if (entry != null) data = data + MathUtils.round(entry.getY()+"", 2);
        LegendEntry ma = new LegendEntry("MA" + para + data, Legend.LegendForm.NONE,
                NaN, NaN, null, mColorMas[i]);
        legendEntries.add(ma);
    }
    Legend legend = mTopChartViewBase.getLegend();
    legend.setCustom(legendEntries);
    legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
}
 
Example #2
Source File: PieChartFrag.java    From Stayfit with Apache License 2.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag_simple_pie, container, false);
    
    mChart = (PieChart) v.findViewById(R.id.pieChart1);
    mChart.setDescription("");
    
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf");
    
    mChart.setCenterTextTypeface(tf);
    mChart.setCenterText(generateCenterText());
    mChart.setCenterTextSize(10f);
    mChart.setCenterTextTypeface(tf);
     
    // radius of the center hole in percent of maximum radius
    mChart.setHoleRadius(45f);
    mChart.setTransparentCircleRadius(50f);
    
    Legend l = mChart.getLegend();
    l.setPosition(LegendPosition.RIGHT_OF_CHART);
    
    mChart.setData(generatePieData());
    
    return v;
}
 
Example #3
Source File: LegendRenderer.java    From iMoney with Apache License 2.0 6 votes vote down vote up
/**
 * Draws the Legend-form at the given position with the color at the given
 * index.
 *
 * @param c     canvas to draw with
 * @param x     position
 * @param y     position
 * @param index the index of the color to use (in the colors array)
 */
protected void drawForm(Canvas c, float x, float y, int index, Legend legend) {

    if (legend.getColors()[index] == ColorTemplate.COLOR_SKIP)
        return;

    mLegendFormPaint.setColor(legend.getColors()[index]);

    float formsize = legend.getFormSize();
    float half = formsize / 2f;

    switch (legend.getForm()) {
        case CIRCLE:
            c.drawCircle(x + half, y, half, mLegendFormPaint);
            break;
        case SQUARE:
            c.drawRect(x, y - half, x + formsize, y + half, mLegendFormPaint);
            break;
        case LINE:
            c.drawLine(x, y, x + formsize, y, mLegendFormPaint);
            break;
    }
}
 
Example #4
Source File: LegendRenderer.java    From NetKnight with Apache License 2.0 6 votes vote down vote up
/**
 * Draws the Legend-form at the given position with the color at the given
 * index.
 *
 * @param c     canvas to draw with
 * @param x     position
 * @param y     position
 * @param index the index of the color to use (in the colors array)
 */
protected void drawForm(Canvas c, float x, float y, int index, Legend legend) {

    if (legend.getColors()[index] == ColorTemplate.COLOR_SKIP)
        return;

    mLegendFormPaint.setColor(legend.getColors()[index]);

    float formsize = legend.getFormSize();
    float half = formsize / 2f;

    switch (legend.getForm()) {
        case CIRCLE:
            c.drawCircle(x + half, y, half, mLegendFormPaint);
            break;
        case SQUARE:
            c.drawRect(x, y - half, x + formsize, y + half, mLegendFormPaint);
            break;
        case LINE:
            c.drawLine(x, y, x + formsize, y, mLegendFormPaint);
            break;
    }
}
 
Example #5
Source File: TempChart.java    From octoandroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * By calling setData initializes the rest of the view
 * @param data the chart data
 */
@Override
public void setData(LineData data) {
    super.setData(data);
    setOnChartValueSelectedListener(this);
    setDescription("");
    setNoDataTextDescription("No chart data");
    setTouchEnabled(true);
    setScaleEnabled(true);
    setDragEnabled(true);
    setDrawGridBackground(true);
    setPinchZoom(true);

    Legend legend = getLegend();
    legend.setForm(Legend.LegendForm.CIRCLE);
    legend.setWordWrapEnabled(true);

    XAxis xAxis = getXAxis();
    xAxis.setAvoidFirstLastClipping(true);
    xAxis.setPosition(XAxis.XAxisPosition.TOP);

    YAxis yAxisLeft = getAxisLeft();
    yAxisLeft.setAxisMinValue(0);

    YAxis yAxisRight = getAxisRight();
    yAxisRight.setEnabled(false);
}
 
Example #6
Source File: LegendRenderer.java    From android-kline with Apache License 2.0 5 votes vote down vote up
public LegendRenderer(ViewPortHandler viewPortHandler, Legend legend) {
    super(viewPortHandler);

    this.mLegend = legend;

    mLegendLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mLegendLabelPaint.setTextSize(Utils.convertDpToPixel(9f));
    mLegendLabelPaint.setTextAlign(Align.LEFT);

    mLegendFormPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mLegendFormPaint.setStyle(Paint.Style.FILL);
}
 
Example #7
Source File: StatisticsFragment.java    From HeartBeat with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPreExecute() {
    mPieChart.setTouchEnabled(false);
    mPieChart.setUsePercentValues(true);
    mPieChart.setDescription("");
    mPieChart.setDrawHoleEnabled(true);
    mPieChart.setHoleColorTransparent(true);
    mPieChart.setTransparentCircleColor(Color.WHITE);
    mPieChart.setHoleRadius(40f);
    mPieChart.setTransparentCircleRadius(45f);
    mPieChart.setDrawCenterText(false);
    Legend legend = mPieChart.getLegend();
    legend.setEnabled(false);
}
 
Example #8
Source File: CalibrationLinearityActivity.java    From NoiseCapture with GNU General Public License v3.0 5 votes vote down vote up
private void initLine() {
    LineChart lineChart = getLineChart();
    if(lineChart == null) {
        return;
    }
    lineChart.setDescription("");

    lineChart.setDrawGridBackground(false);

    // enable scaling and dragging

    Legend l = lineChart.getLegend();
    l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    l.setTextColor(Color.WHITE);

    YAxis yl = lineChart.getAxisLeft();
    yl.setTextColor(Color.WHITE);
    yl.setGridColor(Color.WHITE);
    lineChart.getAxisRight().setEnabled(false);

    XAxis xl = lineChart.getXAxis();
    xl.setDrawGridLines(false);
    xl.setTextColor(Color.WHITE);
    xl.setGridColor(Color.WHITE);
    xl.setPosition(XAxis.XAxisPosition.BOTTOM);
    xl.setDrawAxisLine(true);
    xl.setLabelRotationAngle(-90);
    xl.setDrawLabels(true);
    xl.setLabelsToSkip(0);
}
 
Example #9
Source File: GraphicActivity.java    From ToDay with MIT License 5 votes vote down vote up
private void addDataSet() {
    Log.d(TAG, "addDataSet started");
    ArrayList<PieEntry> yEntrys = new ArrayList<>();
    ArrayList<String> xEntrys = new ArrayList<>();

    for(int i = 0; i < yData.length; i++){
        yEntrys.add(new PieEntry(yData[i] , i));
    }

    for(int i = 1; i < xData.length; i++){
        xEntrys.add(xData[i]);
    }

    //create the data set
    PieDataSet pieDataSet = new PieDataSet(yEntrys, "Төлөвлөгөөний үзүүлэлтлл");
    pieDataSet.setSliceSpace(2);
    pieDataSet.setValueTextSize(12);

    //add colors to dataset
    ArrayList<Integer> colors = new ArrayList<>();
    colors.add(Color.GRAY);
    colors.add(Color.BLUE);
    colors.add(Color.RED);
    colors.add(Color.GREEN);
    colors.add(Color.CYAN);
    colors.add(Color.YELLOW);
    colors.add(Color.MAGENTA);

    pieDataSet.setColors(colors);

    //add legend to chart
    Legend legend = pieChart.getLegend();
    legend.setForm(Legend.LegendForm.CIRCLE);
    legend.setPosition(Legend.LegendPosition.LEFT_OF_CHART);

    //create pie data object
    PieData pieData = new PieData(pieDataSet);
    pieChart.setData(pieData);
    pieChart.invalidate();
}
 
Example #10
Source File: BarChartHelper.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
public BarChart generateBarChartConfig(BarChart barChart) {
    XAxis xAxis = barChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //定制X轴是在图表上方还是下方。
    xAxis.setDrawAxisLine(true);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularity(1f);//放大的时候X值不增多


    YAxis yAxisRight = barChart.getAxisRight();
    yAxisRight.setEnabled(false);
    YAxis yAxisLeft = barChart.getAxisLeft();
    yAxisLeft.setAxisMinimum(0);

    barChart.setDrawBarShadow(false);
    barChart.setPinchZoom(true);
    barChart.setFitBars(true);
    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
    barChart.setMaxVisibleValueCount(60);

    barChart.setDrawValueAboveBar(true);

    barChart.getDescription().setEnabled(false);
    barChart.setNoDataText("无数据");

    Legend l = barChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    //设置单方向和双方向缩放 true x,y方向可以同时控制,false只能控制x方向的缩小放大或者Y方向的缩小放大
    l.setDrawInside(false);
    l.setFormSize(8f);
    l.setXEntrySpace(4f);
    return barChart;
}
 
Example #11
Source File: PieChartFrag.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag_simple_pie, container, false);

    chart = v.findViewById(R.id.pieChart1);
    chart.getDescription().setEnabled(false);

    Typeface tf = Typeface.createFromAsset(context.getAssets(), "OpenSans-Light.ttf");

    chart.setCenterTextTypeface(tf);
    chart.setCenterText(generateCenterText());
    chart.setCenterTextSize(10f);
    chart.setCenterTextTypeface(tf);

    // radius of the center hole in percent of maximum radius
    chart.setHoleRadius(45f);
    chart.setTransparentCircleRadius(50f);

    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);

    chart.setData(generatePieData());

    return v;
}
 
Example #12
Source File: LineChartHelper.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
public LineChart generateLineChartConfig(LineChart lineChart) {

        //XY轴配置
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //定制X轴是在图表上方还是下方。
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1);
        YAxis yAxisRight = lineChart.getAxisRight();
        yAxisRight.setEnabled(false);
        YAxis yAxisLeft = lineChart.getAxisLeft();
        yAxisLeft.setAxisMinimum(0);
        yAxisLeft.setGranularity(1);

        //背景设置
        lineChart.setDrawGridBackground(false);//表格背景绘制
        lineChart.setBackgroundColor(AppContext.getContext().getResources().getColor(R.color.white));
        //Legend定制
        lineChart.getLegend().setPosition(Legend.LegendPosition.ABOVE_CHART_LEFT);
        lineChart.getLegend().setForm(Legend.LegendForm.CIRCLE);//Legend样式
        //图表描述
        lineChart.setDescription(null);
        // 设置无数据文本提示
        lineChart.setNoDataText("暂无数据");
        //设置单方向和双方向缩放 true x,y方向可以同时控制,false只能控制x方向的缩小放大或者Y方向的缩小放大
        lineChart.setPinchZoom(true);
        return lineChart;
    }
 
Example #13
Source File: LegendRenderer.java    From Ticket-Analysis with MIT License 5 votes vote down vote up
public LegendRenderer(ViewPortHandler viewPortHandler, Legend legend) {
    super(viewPortHandler);

    this.mLegend = legend;

    mLegendLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mLegendLabelPaint.setTextSize(Utils.convertDpToPixel(9f));
    mLegendLabelPaint.setTextAlign(Align.LEFT);

    mLegendFormPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mLegendFormPaint.setStyle(Paint.Style.FILL);
}
 
Example #14
Source File: CalibrationLinearityActivity.java    From NoiseCapture with GNU General Public License v3.0 5 votes vote down vote up
private void initScatter() {
    final ScatterChart scatterChart = getScatterChart();
    if(scatterChart == null) {
        return;
    }
    scatterChart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Show
            AlertDialog.Builder builder = new AlertDialog.Builder(CalibrationLinearityActivity.this);
            builder.setTitle(CalibrationLinearityActivity.this.getText(R.string.calibration_select_frequency));
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(CalibrationLinearityActivity.this,
                    R.array.calibrate_type_list_array, android.R.layout.simple_selectable_list_item);
            builder.setAdapter(adapter,
                    new ItemActionOnClickListener(CalibrationLinearityActivity.this, scatterChart));
            builder.show();
        }
    });

    scatterChart.setDescription("");

    scatterChart.setDrawGridBackground(false);

    scatterChart.setMaxVisibleValueCount(200);

    Legend l = scatterChart.getLegend();
    l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    l.setTextColor(Color.WHITE);

    YAxis yl = scatterChart.getAxisLeft();
    yl.setTextColor(Color.WHITE);
    yl.setGridColor(Color.WHITE);
    scatterChart.getAxisRight().setEnabled(false);

    XAxis xl = scatterChart.getXAxis();
    xl.setDrawGridLines(false);
    xl.setTextColor(Color.WHITE);
    xl.setGridColor(Color.WHITE);
}
 
Example #15
Source File: CalibrationLinearityActivity.java    From NoiseCapture with GNU General Public License v3.0 5 votes vote down vote up
private void initBar() {
    BarChart barChart = getBarChart();
    if(barChart == null) {
        return;
    }
    barChart.setDescription("");

    barChart.setDrawGridBackground(false);

    barChart.setMaxVisibleValueCount(200);

    Legend l = barChart.getLegend();
    l.setPosition(Legend.LegendPosition.ABOVE_CHART_LEFT);
    l.setTextColor(Color.WHITE);

    YAxis yl = barChart.getAxisLeft();
    yl.setTextColor(Color.WHITE);
    yl.setGridColor(Color.WHITE);
    barChart.getAxisRight().setEnabled(false);

    XAxis xl = barChart.getXAxis();
    xl.setDrawGridLines(false);
    xl.setTextColor(Color.WHITE);
    xl.setGridColor(Color.WHITE);
    xl.setPosition(XAxis.XAxisPosition.BOTTOM);
    xl.setDrawAxisLine(true);
    xl.setLabelRotationAngle(-90);
    xl.setDrawLabels(true);
    xl.setLabelsToSkip(0);
}
 
Example #16
Source File: LegendRenderer.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
public LegendRenderer(ViewPortHandler viewPortHandler, Legend legend) {
    super(viewPortHandler);

    this.mLegend = legend;

    mLegendLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mLegendLabelPaint.setTextSize(Utils.convertDpToPixel(9f));
    mLegendLabelPaint.setTextAlign(Align.LEFT);

    mLegendFormPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mLegendFormPaint.setStyle(Paint.Style.FILL);
    mLegendFormPaint.setStrokeWidth(3f);
}
 
Example #17
Source File: MainActivity.java    From android-ponewheel with MIT License 5 votes vote down vote up
private void initBatteryChart() {
    mBatteryChart = findViewById(R.id.batteryPieChart);
    // configure pie chart
    mBatteryChart.setUsePercentValues(true);
    mBatteryChart.setDescription(new Description());
    // enable hole and configure
    mBatteryChart.setDrawHoleEnabled(true);
    Legend legend = mBatteryChart.getLegend();
    legend.setEnabled(false);
}
 
Example #18
Source File: BarChartFrag.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag_simple_bar, container, false);
    
    // create a new chart object
    mChart = new BarChart(getActivity());
    mChart.setDescription("");
    mChart.setOnChartGestureListener(this);
    
    MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view);

    mChart.setMarkerView(mv);

    mChart.setDrawGridBackground(false);
    mChart.setDrawBarShadow(false);
    
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"OpenSans-Light.ttf");
    
    mChart.setData(generateBarData(1, 20000, 12));
    
    Legend l = mChart.getLegend();
    l.setTypeface(tf);
    
    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTypeface(tf);
    leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)

    mChart.getAxisRight().setEnabled(false);
    
    XAxis xAxis = mChart.getXAxis();
    xAxis.setEnabled(false);
    
    // programatically add the chart
    FrameLayout parent = (FrameLayout) v.findViewById(R.id.parentLayout);
    parent.addView(mChart);
    
    return v;
}
 
Example #19
Source File: ScatterChartFrag.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag_simple_scatter, container, false);
    
    mChart = (ScatterChart) v.findViewById(R.id.scatterChart1);
    mChart.setDescription("");
    
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"OpenSans-Light.ttf");
    
    MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view);

    mChart.setMarkerView(mv);

    mChart.setDrawGridBackground(false);
    mChart.setData(generateScatterData(6, 10000, 200));
    
    XAxis xAxis = mChart.getXAxis();
    xAxis.setEnabled(true);
    xAxis.setPosition(XAxisPosition.BOTTOM);
    
    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTypeface(tf);
    
    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setTypeface(tf);
    rightAxis.setDrawGridLines(false);
    
    Legend l = mChart.getLegend();
    l.setWordWrapEnabled(true);
    l.setTypeface(tf);
    l.setFormSize(14f);
    l.setTextSize(9f);
    
    // increase the space between legend & bottom and legend & content
    l.setYOffset(13f);       
    mChart.setExtraBottomOffset(16f);
    
    return v;
}
 
Example #20
Source File: BaseChartFragment.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * date: 2019/7/31
 * author: chenli
 * description: 刷新中部图标值
 */
protected void refreshChartLegend(int index){
    Map<String, KlineEntity.DataEntity> dataEntities = mKlineEntity.getData();
    int middleIndex = index;
    if (!CURRENT_DAY_FRAGMENT.equals(mFragmentType)) middleIndex = middleIndex + mBaseIndex;
    KlineEntity.DataEntity dataEntity = dataEntities.get(String.valueOf(middleIndex));
    if (dataEntity != null){
        List<LegendEntry> legendEntriesMiddle = new ArrayList<>();
        legendEntriesMiddle.add(new LegendEntry("OI:"+dataEntity.getClose_oi(), Legend.LegendForm.NONE,
                NaN, NaN, null, mOIColor));
        legendEntriesMiddle.add(new LegendEntry("VOL:"+dataEntity.getVolume(), Legend.LegendForm.NONE,
                NaN, NaN, null, mIncreasingColor));
        Legend legendMiddle = mMiddleChartViewBase.getLegend();
        legendMiddle.setCustom(legendEntriesMiddle);
        legendMiddle.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legendMiddle.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    }

    Float emasF = mEMAs.get(index);
    Float emalF = mEMAl.get(index);
    if (emasF != null && emalF != null){
        float emas = mEMAs.get(index);
        float emal = mEMAl.get(index);
        String dif = MathUtils.round((emas - emal) + "", 2);
        String dea = MathUtils.round(mDEA.get(index) + "", 2);
        List<LegendEntry> legendEntriesBottom = new ArrayList<>();
        legendEntriesBottom.add(new LegendEntry("MACD(12,26,9)" , Legend.LegendForm.NONE,
                NaN, NaN, null, mDeaColor));
        legendEntriesBottom.add(new LegendEntry("DIFF:"+dif, Legend.LegendForm.NONE,
                NaN, NaN, null, mDiffColor));
        legendEntriesBottom.add(new LegendEntry("DEA:"+dea, Legend.LegendForm.NONE,
                NaN, NaN, null, mDeaColor));
        Legend legendBottom = mBottomChartViewBase.getLegend();
        legendBottom.setCustom(legendEntriesBottom);
        legendBottom.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legendBottom.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    }

}
 
Example #21
Source File: PerformanceLineChart.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
private void setData(int count, float range) {

        ArrayList<Entry> values = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * (range + 1)) + 3;
            values.add(new Entry(i * 0.001f, val));
        }

        // create a dataset and give it a type
        LineDataSet set1 = new LineDataSet(values, "DataSet 1");

        set1.setColor(Color.BLACK);
        set1.setLineWidth(0.5f);
        set1.setDrawValues(false);
        set1.setDrawCircles(false);
        set1.setMode(LineDataSet.Mode.LINEAR);
        set1.setDrawFilled(false);

        // create a data object with the data sets
        LineData data = new LineData(set1);

        // set data
        chart.setData(data);

        // get the legend (only possible after setting data)
        Legend l = chart.getLegend();
        l.setEnabled(false);
    }
 
Example #22
Source File: BarChartActivitySinus.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_barchart_sinus);
    
    mSinusData = FileUtils.loadBarEntriesFromAssets(getAssets(),"othersine.txt");

    tvX = (TextView) findViewById(R.id.tvValueCount);

    mSeekBarX = (SeekBar) findViewById(R.id.seekbarValues);

    mChart = (BarChart) findViewById(R.id.chart1);

    mChart.setDrawBarShadow(false);
    mChart.setDrawValueAboveBar(true);

    mChart.setDescription("");

    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
    mChart.setMaxVisibleValueCount(60);

    // scaling can now only be done on x- and y-axis separately
    mChart.setPinchZoom(false);

    // draw shadows for each bar that show the maximum value
    // mChart.setDrawBarShadow(true);

    // mChart.setDrawXLabels(false);

    mChart.setDrawGridBackground(false);
    // mChart.setDrawYLabels(false);

    mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setTypeface(mTf);
    xAxis.setDrawGridLines(false);
    xAxis.setEnabled(false);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTypeface(mTf);
    leftAxis.setLabelCount(6, false);
    leftAxis.setAxisMinValue(-2.5f);
    leftAxis.setAxisMaxValue(2.5f);

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setDrawGridLines(false);
    rightAxis.setTypeface(mTf);
    rightAxis.setLabelCount(6, false);
    rightAxis.setAxisMinValue(-2.5f);
    rightAxis.setAxisMaxValue(2.5f);

    mSeekBarX.setOnSeekBarChangeListener(this);
    mSeekBarX.setProgress(150); // set data

    Legend l = mChart.getLegend();
    l.setPosition(LegendPosition.BELOW_CHART_LEFT);
    l.setForm(LegendForm.SQUARE);
    l.setFormSize(9f);
    l.setTextSize(11f);
    l.setXEntrySpace(4f);

    mChart.animateXY(2000, 2000);
}
 
Example #23
Source File: BarChartActivity.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_barchart);

    tvX = (TextView) findViewById(R.id.tvXMax);
    tvY = (TextView) findViewById(R.id.tvYMax);

    mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
    mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);

    mChart = (BarChart) findViewById(R.id.chart1);
    mChart.setOnChartValueSelectedListener(this);

    mChart.setDrawBarShadow(false);
    mChart.setDrawValueAboveBar(true);

    mChart.setDescription("");

    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
    mChart.setMaxVisibleValueCount(60);

    // scaling can now only be done on x- and y-axis separately
    mChart.setPinchZoom(false);

    mChart.setDrawGridBackground(false);
    // mChart.setDrawYLabels(false);

    mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setTypeface(mTf);
    xAxis.setDrawGridLines(false);
    xAxis.setSpaceBetweenLabels(2);

    YAxisValueFormatter custom = new MyYAxisValueFormatter();

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTypeface(mTf);
    leftAxis.setLabelCount(8, false);
    leftAxis.setValueFormatter(custom);
    leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setSpaceTop(15f);
    leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setDrawGridLines(false);
    rightAxis.setTypeface(mTf);
    rightAxis.setLabelCount(8, false);
    rightAxis.setValueFormatter(custom);
    rightAxis.setSpaceTop(15f);
    rightAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)

    Legend l = mChart.getLegend();
    l.setPosition(LegendPosition.BELOW_CHART_LEFT);
    l.setForm(LegendForm.SQUARE);
    l.setFormSize(9f);
    l.setTextSize(11f);
    l.setXEntrySpace(4f);
    // l.setExtra(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
    // "def", "ghj", "ikl", "mno" });
    // l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
    // "def", "ghj", "ikl", "mno" });

    setData(12, 50);

    // setting data
    mSeekBarY.setProgress(50);
    mSeekBarX.setProgress(12);

    mSeekBarY.setOnSeekBarChangeListener(this);
    mSeekBarX.setOnSeekBarChangeListener(this);

    // mChart.setDrawLegend(false);
}
 
Example #24
Source File: StackedBarActivity.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	setContentView(R.layout.activity_barchart);

	tvX = (TextView) findViewById(R.id.tvXMax);
	tvY = (TextView) findViewById(R.id.tvYMax);

	mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
	mSeekBarX.setOnSeekBarChangeListener(this);

	mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
	mSeekBarY.setOnSeekBarChangeListener(this);

	mChart = (BarChart) findViewById(R.id.chart1);
	mChart.setOnChartValueSelectedListener(this);

	mChart.setDescription("");

	// if more than 60 entries are displayed in the chart, no values will be
	// drawn
	mChart.setMaxVisibleValueCount(60);

	// scaling can now only be done on x- and y-axis separately
	mChart.setPinchZoom(false);

	mChart.setDrawGridBackground(false);
	mChart.setDrawBarShadow(false);

	mChart.setDrawValueAboveBar(false);

	// change the position of the y-labels
	YAxis leftAxis = mChart.getAxisLeft();
	leftAxis.setValueFormatter(new MyYAxisValueFormatter());
	leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)
	mChart.getAxisRight().setEnabled(false);

	XAxis xLabels = mChart.getXAxis();
	xLabels.setPosition(XAxisPosition.TOP);

	// mChart.setDrawXLabels(false);
	// mChart.setDrawYLabels(false);

	// setting data
	mSeekBarX.setProgress(12);
	mSeekBarY.setProgress(100);

	Legend l = mChart.getLegend();
	l.setPosition(LegendPosition.BELOW_CHART_RIGHT);
	l.setFormSize(8f);
	l.setFormToTextSpace(4f);
	l.setXEntrySpace(6f);

	// mChart.setDrawLegend(false);
}
 
Example #25
Source File: StatActivity.java    From ankihelper with GNU General Public License v3.0 4 votes vote down vote up
private void drawLastDaysChart(int[][] data) {
    List<Entry> lookupEntries = new ArrayList<>();
    List<Entry> cardaddEntries = new ArrayList<>();
    for(int i = 0; i < data[0].length; i ++){
        lookupEntries.add(new Entry(i, data[1][i]));
        cardaddEntries.add(new Entry(i, data[2][i]));
    }
    if(lookupEntries.size() <= 1){
        mLastDaysChart.setVisibility(View.INVISIBLE);
    }else{
        mLastDaysChart.setVisibility(View.VISIBLE);
    }
    float lineWidth = 2;
    LineDataSet lineDataSet2 = new LineDataSet(lookupEntries, "Lookups");
    lineDataSet2.setColor(DARK_PINK);
    lineDataSet2.setLineWidth(lineWidth);
    lineDataSet2.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
    lineDataSet2.setDrawCircles(false);
    lineDataSet2.setDrawValues(false);
    LineDataSet lineDataSet3 = new LineDataSet(cardaddEntries, "Cards");
    lineDataSet3.setColor(DARK_GREEN);
    lineDataSet3.setLineWidth(lineWidth);
    lineDataSet3.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
    lineDataSet3.setDrawCircles(false);
    lineDataSet3.setDrawValues(false);
    List<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
    dataSets.add(lineDataSet2);
    dataSets.add(lineDataSet3);
    mLastDaysChart.setData(new LineData(dataSets));
    mLastDaysChart.getDescription().setText("");
    //mHourChart.getDescription().setTextAlign();
    mLastDaysChart.getXAxis().setDrawGridLines(false);
    mLastDaysChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    mLastDaysChart.getAxisRight().setEnabled(false);
    //mHourChart.getAxisLeft().setDrawGridLines(false);
    mLastDaysChart.getXAxis().setValueFormatter(
            new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    return  + ((int)(-mLastDays + value + 1)) + "d";
                }
            }
    );
    mLastDaysChart.getLegend().setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
    mLastDaysChart.invalidate();
}
 
Example #26
Source File: BaseDataSet.java    From android-kline with Apache License 2.0 4 votes vote down vote up
@Override
public Legend.LegendForm getForm() {
    return mForm;
}
 
Example #27
Source File: ChartUtil.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 显示图表
 *
 * @param context    上下文
 * @param lineChart  图表对象
 * @param yDataList  数据
 * @param title      图表标题(如:XXX趋势图)
 * @param curveLable 曲线图例名称(如:--用电量/时间)
 * @param unitName   坐标点击弹出提示框中数字单位(如:KWH)
 */
public static void showChart(Context context, LineChart lineChart,
                             List<Entry> yDataList, String title, String curveLable, String unitName) {
    // 设置数据
    lineChart.setData(setLineData(context, yDataList, curveLable));
    // 描述的显示和隐藏
    lineChart.getDescription().setEnabled(false);
    // 是否在折线图上添加边框
    lineChart.setDrawBorders(true);
    // 如果没有数据的时候,会显示这个,类似文本框的placeholder
    lineChart.setNoDataText(context.getString(R.string.no_data_chart));
    // 是否显示表格颜色
    lineChart.setDrawGridBackground(false);
    // 禁止绘制图表边框的线
    lineChart.setDrawBorders(false);
    // 表格的的颜色,在这里是是给颜色设置一个透明度
    // lineChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF);
    // 设置是否启动触摸响应
    lineChart.setTouchEnabled(false);
    // 是否可以拖拽
    lineChart.setDragEnabled(false);
    // 是否可以缩放
    lineChart.setScaleEnabled(false);
    // 如果禁用,可以在x和y轴上分别进行缩放
    lineChart.setPinchZoom(false);
    // lineChart.setMarkerView(mv);
    // 设置背景色
    // lineChart.setBackgroundColor(getResources().getColor(R.color.bg_white));
    // 图例对象
    Legend mLegend = lineChart.getLegend();
    // mLegend.setPosition(LegendPosition.BELOW_CHART_CENTER);
    // 图例样式 (CIRCLE圆形;LINE线性;SQUARE是方块)
    mLegend.setForm(LegendForm.SQUARE);
    // 图例大小
    mLegend.setFormSize(8f);
    // 图例上的字体颜色
    mLegend.setTextColor(context.getApplicationContext().getResources().getColor(R.color.theme_color));
    mLegend.setTextSize(12f);
    // 图例字体
    // mLegend.setTypeface(mTf);
    // 图例的显示和隐藏
    mLegend.setEnabled(false);
    // 隐藏右侧Y轴(只在左侧的Y轴显示刻度)
    lineChart.getAxisRight().setEnabled(false);
    // 隐藏左侧Y轴(只在左侧的Y轴显示刻度)
    lineChart.getAxisLeft().setEnabled(false);
    XAxis xAxis = lineChart.getXAxis();
    // 显示X轴上的刻度值
    xAxis.setDrawLabels(false);
    // 设置X轴的数据显示在报表的下方
    xAxis.setPosition(XAxisPosition.BOTTOM);
    // 轴线
    xAxis.setDrawAxisLine(false);
    // 设置不从X轴发出纵向直线
    xAxis.setDrawGridLines(false);
    // 执行的动画,x轴(动画持续时间)
    lineChart.animateX(1500);
    lineChart.notifyDataSetChanged();
}
 
Example #28
Source File: BaseDataSet.java    From android-kline with Apache License 2.0 4 votes vote down vote up
public void setForm(Legend.LegendForm form) {
    mForm = form;
}
 
Example #29
Source File: BaseDataSet.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
public void setForm(Legend.LegendForm form) {
    mForm = form;
}
 
Example #30
Source File: LegendRenderer.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
/**
 * Draws the Legend-form at the given position with the color at the given
 * index.
 *
 * @param c      canvas to draw with
 * @param x      position
 * @param y      position
 * @param entry  the entry to render
 * @param legend the legend context
 */
protected void drawForm(
        Canvas c,
        float x, float y,
        LegendEntry entry,
        Legend legend) {

    if (entry.formColor == ColorTemplate.COLOR_SKIP ||
            entry.formColor == ColorTemplate.COLOR_NONE ||
            entry.formColor == 0)
        return;

    int restoreCount = c.save();

    Legend.LegendForm form = entry.form;
    if (form == Legend.LegendForm.DEFAULT)
        form = legend.getForm();

    mLegendFormPaint.setColor(entry.formColor);

    final float formSize = Utils.convertDpToPixel(
            Float.isNaN(entry.formSize)
                    ? legend.getFormSize()
                    : entry.formSize);
    final float half = formSize / 2f;

    switch (form) {
        case NONE:
            // Do nothing
            break;

        case EMPTY:
            // Do not draw, but keep space for the form
            break;

        case DEFAULT:
        case CIRCLE:
            mLegendFormPaint.setStyle(Paint.Style.FILL);
            c.drawCircle(x + half, y, half, mLegendFormPaint);
            break;

        case SQUARE:
            mLegendFormPaint.setStyle(Paint.Style.FILL);
            c.drawRect(x, y - half, x + formSize, y + half, mLegendFormPaint);
            break;

        case LINE:
        {
            final float formLineWidth = Utils.convertDpToPixel(
                    Float.isNaN(entry.formLineWidth)
                            ? legend.getFormLineWidth()
                            : entry.formLineWidth);
            final DashPathEffect formLineDashEffect = entry.formLineDashEffect == null
                    ? legend.getFormLineDashEffect()
                    : entry.formLineDashEffect;
            mLegendFormPaint.setStyle(Paint.Style.STROKE);
            mLegendFormPaint.setStrokeWidth(formLineWidth);
            mLegendFormPaint.setPathEffect(formLineDashEffect);

            mLineFormPath.reset();
            mLineFormPath.moveTo(x, y);
            mLineFormPath.lineTo(x + formSize, y);
            c.drawPath(mLineFormPath, mLegendFormPaint);
        }
            break;
    }

    c.restoreToCount(restoreCount);
}