Skip to content

line data set position center in combine chart MPAndroidChart #5214

Open
@Anayaali2019

Description

@Anayaali2019

ISSUE IS HOW TO SET POSISTION OF LINE DATA(PRICE DATA) in CENTER
IF IS SET
YAxis leftAxis = combinedChart.getAxisLeft();

it move complete chart to Center

Legend legend = combinedChart.getLegend();
legend.setEnabled(true);
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);

// Define custom legend entries for red, green, blue, and purple boxes
LegendEntry redEntry = new LegendEntry();
redEntry.formColor = getResources().getColor(android.R.color.holo_red_dark);
redEntry.label = "Volume"; // Label for red box

    LegendEntry greenEntry = new LegendEntry();
    greenEntry.formColor = getResources().getColor(android.R.color.holo_green_dark);
    greenEntry.label = "Volume"; // Label for green box

    LegendEntry blueEntry = new LegendEntry();
    blueEntry.formColor = getResources().getColor(android.R.color.holo_blue_dark);
    blueEntry.label = "OI"; // Label for blue box

    LegendEntry blackEntry = new LegendEntry();
    blackEntry.formColor = getResources().getColor(android.R.color.holo_purple);
    blackEntry.label = "Price"; // Label for purple box

    legend.setCustom(Arrays.asList(redEntry, greenEntry, blueEntry, blackEntry));
    combinedChart.getDescription().setEnabled(false);

// Optional: Add animations and other customizations
combinedChart.animateY(1000); // Animate vertically over 1000ms

    CombinedData combinedData = new CombinedData();

// Get BarData (Volume) and assign it to the left Y-Axis
BarData volumeData = getVolume(modelclassIOdata);
volumeData.setBarWidth(0.9f); // Set bar width for better visualization
combinedData.setData(volumeData); // Adding bar data to combined data

// Get LineData (Price) and assign it to the right Y-Axis
LineData priceData = getPrice(modelclassIOdata);
YAxis priceYAxis = new YAxis();
priceYAxis.setAxisMinimum(miniprice - 50); // Adjust range to show the price line in the middle
priceYAxis.setAxisMaximum(maxprice + 50); // Set the range for price chart

// Get LineData (Open Interest) and assign it to the right Y-Axis
LineData oiData = getOI(modelclassIOdata);

    LineData lineData = new LineData();

    // Add both datasets to the lineData
    lineData.addDataSet(priceData.getDataSetByIndex(0)); // Price data set
    lineData.addDataSet(oiData.getDataSetByIndex(0)); // Open Interest data set


    // Set the combined LineData to combinedData
    combinedData.setData(lineData);


    priceData.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            return String.format(Locale.US, "%.5f", value);  // Ensure high precision
        }
    });

// Update the CombinedChart with the new data
combinedChart.setData(combinedData);

// Refresh the chart to reflect the changes
combinedChart.invalidate();

price data can contain value in -15222 , .2000 , 15000 for different pairs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions