如何在所选项目的微调框上显示饼图?

android charts spinner bar-chart pie-chart

420 观看

1回复

38 作者的声誉

如何在所选项目的微调框上显示饼图?我创建了一个在Spinner OnItem Selected事件上显示条形图和饼图的应用程序。条形图在选定的项目上工作正常。问题是当我选择微调器上的饼图时,应用程序崩溃。帮帮我 。

主要活动

package saro.example.com.chart_view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.ActionBarActivity;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements
        AdapterView.OnItemSelectedListener {

    String[] chart = {"Select" ,"Bar Chart", "Pie Chart"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Getting the instance of Spinner and applying OnItemSelectedListener on it
        Spinner spin = (Spinner) findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);

        //Creating the ArrayAdapter instance having the country list
        ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,chart);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        spin.setAdapter(aa);


    }


    //Performing action onItemSelected and onNothing selected
    @Override
    public void onItemSelected(AdapterView<?> Position, View arg1, int position,long id) {
        String selectedItem = Position.getItemAtPosition(position).toString();
        switch(selectedItem){
            case "Select":
                Toast.makeText(getApplicationContext(),"Please Select Bar Chart or Pie chart",Toast.LENGTH_SHORT).show();
                break;

            case "Bar Chart":
                Intent intent = new Intent(MainActivity.this,barchart.class);
                startActivity(intent);
                break;

            case "Pie Chart":
                Intent intent1 = new Intent(MainActivity.this,PieChart.class);
                startActivity(intent1);
                break;
        }


//        if(selectedItem.equals("Bar Chart"))
//        {
//            Intent intent = new Intent(MainActivity.this,barchart.class);
//            startActivity(intent);
//
//        }
//        else {
//            if(selectedItem.equals("Pie Chart")) {
//                Intent ind = new Intent(MainActivity.this,piechart.class);
//                startActivity(ind);
//            }
            }










    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }




}

barchart.java

package saro.example.com.chart_view;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;
import android.support.v7.app.ActionBarActivity;


import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by saravanakumars on 9/14/2017.
 */

public class barchart extends Activity
{ BarChart chart ;
    ArrayList<BarEntry> BARENTRY ;
    ArrayList<String> BarEntryLabels ;
    BarDataSet Bardataset ;
    BarData BARDATA ;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bar_chart);
//        Toast.makeText(getApplicationContext(),"bar", Toast.LENGTH_LONG).show();



            chart = (BarChart) findViewById(R.id.bar);

            BARENTRY = new ArrayList<>();

            BarEntryLabels = new ArrayList<String>();

            AddValuesToBARENTRY();

            AddValuesToBarEntryLabels();

            Bardataset = new BarDataSet(BARENTRY, "Projects");

            BARDATA = new BarData(BarEntryLabels, Bardataset);

            Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

            chart.setData(BARDATA);

            chart.animateY(3000);

        }

    public void AddValuesToBARENTRY(){

        BARENTRY.add(new BarEntry(2f, 0));
        BARENTRY.add(new BarEntry(4f, 1));
        BARENTRY.add(new BarEntry(6f, 2));
        BARENTRY.add(new BarEntry(8f, 3));
        BARENTRY.add(new BarEntry(7f, 4));
        BARENTRY.add(new BarEntry(3f, 5));

    }

    public void AddValuesToBarEntryLabels(){

        BarEntryLabels.add("January");
        BarEntryLabels.add("February");
        BarEntryLabels.add("March");
        BarEntryLabels.add("April");
        BarEntryLabels.add("May");
        BarEntryLabels.add("June");

    }
}

piechart.java

package saro.example.com.chart_view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ViewPortHandler;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;

import static android.R.attr.data;

/**
 * Created by saravanakumars on 9/14/2017.
 */


    public class piechart extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pie_chart);
//        Toast.makeText(getApplicationContext(), "Pie", Toast.LENGTH_LONG).show();
        PieChart mChart = (PieChart)findViewById(R.id.chart_detail);
        mChart.setUsePercentValues(true);
        mChart.setDescription("");
        mChart.setExtraOffsets(15, 15, 22,20);
        mChart.setDragDecelerationFrictionCoef(0.95f);
        mChart.setDrawSliceText(false);
        mChart.setDrawHoleEnabled(true);
        mChart.setHoleColor(Color.WHITE);
        mChart.setTransparentCircleColor(Color.WHITE);
        mChart.setTransparentCircleAlpha(10);
        mChart.setDrawCenterText(true);
        mChart.setRotationAngle(0);
        mChart.setHoleRadius(55f);
        mChart.setTransparentCircleRadius(1f);
        mChart.setCenterTextRadiusPercent(1f);
        mChart.setPressed(false);
        // enable rotation of the chart by touch
        mChart.setRotationEnabled(false);
        mChart.setHighlightPerTapEnabled(true);
        mChart.setCenterText(" ");
        //*//*data setting*//*
        String[] mParties = new String[]{
                "Groceries", "Entertainment", "Dining", "Appareal"};
        //float mult = 100;
        ArrayList<Entry> yVals1 = new ArrayList<Entry>();
        // IMPORTANT: In a PieChart, no values (Entry) should have the same
        // xIndex (even if from different DataSets), since no values can be
        // drawn above each other.
        yVals1.add(new Entry((float) 16, 0));
        yVals1.add(new Entry((float) 32, 1));
        yVals1.add(new Entry((float) 34, 2));
        yVals1.add(new Entry((float) 18, 3));
        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < 3 + 1; i++)
            xVals.add(mParties[i % mParties.length]);
        PieDataSet dataSet = new PieDataSet(yVals1,"   ");
        //  dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(3f);
        dataSet.setValueTextColor(Color.BLUE);
        // add colors
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(Color.rgb(0, 222, 197));
        colors.add(Color.rgb(40, 168, 239));
        colors.add(Color.rgb(163, 97, 255));
        colors.add(Color.rgb(113, 93, 178));
        dataSet.setColors(colors);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setValueLinePart1Length(.28f);
        dataSet.setValueLinePart2Length(.22f);
        dataSet.setValueLineWidth(.0000001f);
        PieData data = new PieData(xVals, dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.BLACK);
        // data.setValueTypeface(tf);
        mChart.setData(data);
        // undo all highlights
        mChart.highlightValues(null);
        mChart.invalidate();
        mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
        Legend l = mChart.getLegend();
        l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
        l.setEnabled(true);
        l.setTextSize(5f);
        final ArrayList<String> month = new ArrayList<>();
        month.add("Month");        month.add("January");
        month.add("Feb");
        month.add("March");
        month.add("May");
        month.add("June");
        month.add("July");
        month.add("August");
        month.add("September");
        month.add("October");
        month.add("November");
        month.add("December");
    }

    }

gradle.app

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "saro.example.com.chart_view"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    maven { url "https://jitpack.io" }

}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.github.PhilJay:MPAndroidChart:v2.0.9'
    compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
    compile 'com.github.PhilJay:MPAndroidChart:v2.2.2'
    compile 'com.github.PhilJay:MPAndroidChart:v2.1.6'
    compile 'com.github.PhilJay:MPAndroidChart:v2.2.5'
    testCompile 'junit:junit:4.12'
}

提前致谢。错误错误

09-15 11:21:04.646 10243-10243 / saro.example.com.chart_view E / AndroidRuntime:致命例外:主进程:saro.example.com.chart_view,PID:10243 android.content.ActivityNotFoundException:无法找到显式活动类{saro.example.com.chart_view / com.github.mikephil.charting.charts.PieChart};您是否在AndroidManifest.xml中声明了此活动?在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)在android.app.Instrumentation.execStartActivity(Instrumentation.java:

作者: Saravana 的来源 发布者: 2017 年 9 月 15 日

回应 1


0

61 作者的声誉

根据您的日志记录,您错过了在中声明活动的机会Manifest。实际上,我认为这是来自github回购的活动,您已将其编译为项目。检查该库的文档,也许您必须从​​中声明一些活动

作者: kostyaBoss 发布者: 2017 年 9 月 15 日
32x32