Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MP Android图表-如何设置条形图视图中显示的默认条数?_Java_Android_Charts_Bar Chart_Mpandroidchart - Fatal编程技术网

Java MP Android图表-如何设置条形图视图中显示的默认条数?

Java MP Android图表-如何设置条形图视图中显示的默认条数?,java,android,charts,bar-chart,mpandroidchart,Java,Android,Charts,Bar Chart,Mpandroidchart,我是android开发新手,目前正在学习如何构建图表,希望最终连接到DB并创建动态图表。我使用了MPAndroid图表GitHub页面()中的一个示例 我试图操纵它来满足我的需求 现在我的图表有一个x轴(天)和一个y轴(加仑) 为简单起见,我创建了一个字符串数组“Day1”到“Day30”,用作X值。Y值是一个随机数(只是为了查看不同的条大小) 目前: 我的图表以随机Y值显示所有30天,我可以放大和缩小,以及放大时向左或向右滚动 我想要的是: 构建图表时,我希望图表的默认视图仅显示7天(例如“D

我是android开发新手,目前正在学习如何构建图表,希望最终连接到DB并创建动态图表。我使用了MPAndroid图表GitHub页面()中的一个示例 我试图操纵它来满足我的需求

现在我的图表有一个x轴(天)和一个y轴(加仑)

为简单起见,我创建了一个字符串数组“Day1”到“Day30”,用作X值。Y值是一个随机数(只是为了查看不同的条大小)

目前: 我的图表以随机Y值显示所有30天,我可以放大和缩小,以及放大时向左或向右滚动

我想要的是: 构建图表时,我希望图表的默认视图仅显示7天(例如“Day5”到“Day11”),但仍然能够在图表上向左或向右滚动以查看其他1-30天。另外,我希望能够从7天缩小到最多30天,或者放大到至少3天

这是我目前的java文件。我知道现在很邋遢,我正在学习

公共类ConsumptionActivity扩展了AppCompatActivity
实现NavigationView.OnNavigationItemSelectedListener、OnChartValueSelectedListener、OnSeekbarChangListener{
受保护的柱状图麦克哈特;
私人SeekBar mSeekBarX,mSeekBarY;
私有文本视图tvX、tvY;
私人字体mTf;
受保护字符串[]mDays=新字符串[]{
“第1天”、“第2天”、“第3天”、“第4天”、“第5天”、“第6天”、“第7天”、“第8天”、“第9天”、“第10天”、“第11天”、“第12天”、“第13天”、“第14天”、“第15天”,
“第16天”、“第17天”、“第18天”、“第19天”、“第20天”、“第21天”、“第22天”、“第23天”、“第24天”、“第25天”、“第26天”、“第27天”、“第28天”、“第29天”、“第30天”};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_consumption);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。设置抽屉定位器(开关);
toggle.syncState();
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
Intent=getIntent();
LoginAuthentification loggedIn=(LoginAuthentification)intent.getExtras().getSerializable(“LoginClass”);
微调器微调器=(微调器)findViewById(R.id.accountDropdown);
SpinnerAdapter snprAdapter=新的ArrayAdapter(此,R.layout.support_simple_spinner_dropdown_项,loggedIn.getAccountList());
旋转器。设置适配器(snprAdapter);
//使用字符串数组和默认微调器布局创建ArrayAdapter
/*ArrayAdapter=ArrayAdapter.createFromResource(此,
R.array.test、android.R.layout.simple\u微调器\u项);
//指定显示选项列表时要使用的布局
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
//将适配器应用于微调器
旋转器。设置适配器(适配器)*/
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
//已选择一个项目。您可以使用
//父项getItemAtPosition(pos)
}
@凌驾
未选择公共无效(AdapterView父级){
//另一个接口回调
}
});
ObjectMapper mapper=新的ObjectMapper();
APICalls accountInfo=新的APICalls();
AccountDetails accountInUse=新的AccountDetails();
试一试{
accountInUse=mapper.readValue(String.valueOf(accountInfo.AccountDetails(loggedIn.getToken(),loggedIn.getAccountList().get(0))),AccountDetails.class);
}捕获(例外e){
e、 printStackTrace();
}
/*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=(条形图)findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(此);
mChart.settrabarshadow(假);
mChart.SetDrawValueUpperBar(真实);
mChart.setDescription(“用水详情”);
麦克哈特动画公司(80008000);
//如果图表中显示的条目超过60个,则不会显示任何值
//画
mChart.setMaxVisibleValueCount(60);
//现在只能分别在x轴和y轴上进行缩放
mChart.setPinchZoom(假);
mChart.setDrawGridBackground(真);
//mChart.setDrawYLabels(假);
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自定义=新的MyYAxisValueFormatter();
YAxis leftAxis=mChart.getAxisLeft();
leftAxis.setTypeface(mTf);
leftAxis.setLabelCount(8,false);
leftAxis.setValueFormatter(自定义);
leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_图表);
左轴。设置空间顶部(15f);
leftAxis.setAxisMinValue(0f);//这将替换setStartZero(true)
YAxis rightAxis=mChart.getAxisRight();
rightAxis.setDrawGridLines(假);
设置字体(mTf);
rightAxis.se
public class ConsumptionActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, OnChartValueSelectedListener, OnSeekBarChangeListener {


    protected BarChart mChart;
    private SeekBar mSeekBarX, mSeekBarY;
    private TextView tvX, tvY;

    private Typeface mTf;
    protected String[] mDays = new String[] {
            "Day 1","Day 2","Day 3","Day 4","Day 5","Day 6","Day 7","Day 8","Day 9","Day 10","Day 11","Day 12","Day 13","Day 14","Day 15",
            "Day 16","Day 17","Day 18","Day 19","Day 20","Day 21","Day 22","Day 23","Day 24","Day 25","Day 26","Day 27","Day 28","Day 29","Day 30"};



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_consumption);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


        setSupportActionBar(toolbar);



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        Intent intent = getIntent();
        LoginAuthentification loggedIn = (LoginAuthentification) intent.getExtras().getSerializable("LoginClass");


        Spinner spinner = (Spinner) findViewById(R.id.accountDropdown);

        SpinnerAdapter snprAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, loggedIn.getAccountList());


        spinner.setAdapter(snprAdapter);

// Create an ArrayAdapter using the string array and a default spinner layout
        /*ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.test, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);*/
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // An item was selected. You can retrieve the selected item using
                // parent.getItemAtPosition(pos)
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });

        ObjectMapper mapper = new ObjectMapper();
        APICalls accountInfo = new APICalls();
        AccountDetails accountInUse = new AccountDetails();


        try {
            accountInUse = mapper.readValue(String.valueOf(accountInfo.AccountDetails(loggedIn.getToken(),loggedIn.getAccountList().get(0))), AccountDetails.class);
        } catch (Exception e) {
            e.printStackTrace();
        }


        /*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("Water Consumption Details");
        mChart.animateXY(8000, 8000);
        // 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(true);
        // 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(Legend.LegendPosition.BELOW_CHART_LEFT);
        l.setForm(Legend.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(30, 50);

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

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

        // mChart.setDrawLegend(false);




/*
        APICalls callsForAll = new APICalls();
        try {
            callsForAll.AccountDetails(token.get(0),token.get(1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        final TextView nameView = (TextView) findViewById(R.id.name);

*/
    }




    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.consumption, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_Alerts) {

        } else if (id == R.id.nav_settings) {

        }
          else if (id == R.id.nav_Logout){

           // LOGOUT LOGIC SHOULD GO HERE
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(1);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

   @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    private void setData(int count, float range) {

        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < count; i++) {
            xVals.add(mDays[i]);
        }

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

        for (int i = 0; i < count; i++) {
            float mult = (range + 1);
            float val = (float) (Math.random() * mult);
            yVals1.add(new BarEntry(val, i));
        }

        BarDataSet set1 = new BarDataSet(yVals1, "DataSet");
        set1.setBarSpacePercent(35f);
        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);
        set1.setColors(new int[]{R.color.neptuneOrange,R.color.colorPrimary});

        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(10f);
        data.setValueTypeface(mTf);

        mChart.setData(data);
    }

    @SuppressLint("NewApi")
    @Override
    public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

        if (e == null)
            return;

        RectF bounds = mChart.getBarBounds((BarEntry) e);
        PointF position = mChart.getPosition(e, AxisDependency.LEFT);

        Log.i("bounds", bounds.toString());
        Log.i("position", position.toString());

        Log.i("x-index",
                "low: " + mChart.getLowestVisibleXIndex() + ", high: "
                        + mChart.getHighestVisibleXIndex());
    }

    public void onNothingSelected() {
    };
}