MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Horizontal Bar Graph Rendering Questions - Inverted Axis?

Open troy21688 opened this issue 10 years ago • 3 comments

I have successfully generated a Horizontal bar graph with the following code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO: Add Fragment Code to check if savedInstanceState == null; add at Activity Level?


    // Inflate the layout for this fragment
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_poll, container, false);
    getActivity().setTitle(R.string.todays_polls_title);

    //Initialize Poll Results Bar Chart and set to Invisible
    mPollResults = (HorizontalBarChart) rootView.findViewById(R.id.poll_results_chart);
    mPollResults.setBackgroundColor(getResources().getColor(R.color.white));
    mPollResults.setNoDataTextDescription(getResources().getString(R.string.no_results_description));
    mPollResults.setVisibility(View.INVISIBLE);

    mPollQuestion = (TextView) rootView.findViewById(R.id.poll_question);
    mPollQuestionRadioGroup = (RadioGroup) rootView.findViewById(R.id.poll_question_group);
    mPollQuestionRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            group.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mPollQuestionRadioGroup.setVisibility(View.INVISIBLE);
                }
            }, 1000);

            mPollResults.setVisibility(View.VISIBLE);

            //Pollvalues get added here
            ArrayList<BarEntry> valuesToCompare = new ArrayList<BarEntry>();
            valuesToCompare.add(new BarEntry(100.000f, 0));
            valuesToCompare.add(new BarEntry(1000.000f, 1));
            valuesToCompare.add(new BarEntry(75.000f, 2));
            BarDataSet data = new BarDataSet(valuesToCompare, "Vote Results");

            data.setColors(new int[]{getResources().getColor(R.color.black)});
            data.setAxisDependency(YAxis.AxisDependency.LEFT);

            //xAxis is a inverted yAxis since the graph is horizontal
            XAxis xAxis = mPollResults.getXAxis();
            xAxis.setDrawGridLines(false);
            xAxis.setDrawAxisLine(false);
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

            //yAxis is an inverted xAxis since the graph is horizontal
            YAxis yAxisLeft = mPollResults.getAxisLeft();
            yAxisLeft.setDrawGridLines(false);
            yAxisLeft.setEnabled(false);
            YAxis yAxisRight = mPollResults.getAxisRight();
            yAxisRight.setDrawGridLines(false);
            yAxisRight.setEnabled(false);

            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(data);

            //Poll Answer Options get added here
            ArrayList<String> yVals = new ArrayList<String>();
            yVals.add(0, "Test 1");
            yVals.add(1, "Test 2");
            yVals.add(2, "Test 3");
            BarData testBarData = new BarData(yVals, dataSets);

            mPollResults.setTouchEnabled(false);
            mPollResults.setPinchZoom(false);
            mPollResults.setData(testBarData);
            mPollResults.invalidate();
        }
    });

image

However, I am having a few issues:

  1. It appears that the X axis and Y axis are inverted. Originally, the "Test 1," "Test 2," and "Test 3" labels were on the right side of the graph, however I set the position to "BOTTOM" to successfully move the labels to the left side of the graph. Am I rendering the axis incorrectly?
  2. How can I remove that description text in the lower right (I found no setEnabled() method in the documentation)? If I don't want to remove it, how can I change what it says (I am assuming a Chart.setDescription method based on the documentation) and its position?
  3. I am concerned about screen space and would like the left Y axis to have labels "1," "2," and "3," with a key that lists the actual description of these labels. The longer the descriptions, the less space I have for the actual bars. Is this feature possible? If not, what is the best recommendation to optimize the left Y axis screen space?
  4. I am eventually going to change this data in real time, and I still need to read up on the documentation. I want to better understand how each chart is saved. I am assuming the removal of a chart saves it on the client side (the client's device or tablet). Is there a way to optimize the use of charts/images to prevent them from piling up in memory (other than using a server)?

troy21688 avatar Feb 12 '16 20:02 troy21688

  1. You did everything correct. If you want to change the order or the axis labels, you can simply add them in a different order to your array.
  2. chart.setDescription(...) allows you to change the text and also remove it (by providing "")
  3. You can dynamically change the length of the labels by simply exchanging the array and re-rendering the graph. Other than that, it is possible to rotate the labels.
  4. Realtime-data documentation. I am not sure what you mean by the rest.

PhilJay avatar Feb 12 '16 23:02 PhilJay

Thanks Phil,

Regarding #4, I am just trying to determine where the images of the graphs are actually stored. If I have a real-time graphed based on results of users voting or performing some action, the graph is going to update every second. Each graph is an image, and I am curious where this image data actually gets stored.

On Fri, Feb 12, 2016 at 5:52 PM, Philipp Jahoda [email protected] wrote:

  1. You did everything correct. If you want to change the order or the axis labels, you can simply add them in a different order to your array.
  2. chart.setDescription(...) allows you to change the text and also remove it (by providing "")
  3. You can dynamically change the length of the labels by simply exchanging the array and re-rendering the graph. Other than that, it is possible to rotate the labels.
  4. Realtime-data documentation https://github.com/PhilJay/MPAndroidChart/wiki/Dynamic-&-Realtime-Data. I am not sure what you mean by the rest.

— Reply to this email directly or view it on GitHub https://github.com/PhilJay/MPAndroidChart/issues/1474#issuecomment-183532353 .

Troy Chuinard, CPA

troy21688 avatar Feb 13 '16 04:02 troy21688

@PhilJay How can i rotate the y-axis label by 90 degree in horizontal bar chart?? xAxis.setLabelRotationAngle(-90) will rotate the x -axis label by 90 degree.But how to rotate Y-axis label?

MyGitUserAcc avatar Mar 02 '17 07:03 MyGitUserAcc