Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to align yAxis at zero point? #2989

Closed
jonasman opened this issue Nov 13, 2017 · 4 comments
Closed

How to align yAxis at zero point? #2989

jonasman opened this issue Nov 13, 2017 · 4 comments

Comments

@jonasman
Copy link

I have the following line chart with 2 datasets. One data set is dependent on the left yAxis the other one on the right yAxis.

Now i want them both to have the zero value in the same line.
The rest of the chart doesn't need to be aligned, only the zero line.
How can i do that?

Code:

    let speedLine = LineChartDataSet(values: speedEntries, label: "Speed")
    speedLine.colors = [UIColor.white]
    speedLine.drawCirclesEnabled = false
    speedLine.axisDependency = .left

    let powerLine = LineChartDataSet(values: powerEntries, label: "Power")
    powerLine.colors = [UIColor.green]
    powerLine.drawCirclesEnabled = false
    powerLine.axisDependency = .right

    let data = LineChartData()
    data.addDataSet(speedLine)
    data.addDataSet(powerLine)

    chartView.data = data
    chartView.xAxis.labelTextColor = UIColor.white
    chartView.xAxis.labelCount = 20
    chartView.leftAxis.labelTextColor = UIColor.white
    chartView.rightAxis.labelTextColor = UIColor.green
    chartView.legend.textColor = UIColor.white

    chartView.chartDescription?.text = ""

iczja

@liuxuan30
Copy link
Member

liuxuan30 commented Nov 15, 2017

because you are using different axises, so the axis range is different as well. you have to make sure both axis share the same axis range, like setting axisMinimum and axisMaximum.

@jonasman
Copy link
Author

Hi @liuxuan30 maybe i didnt explain correctly.

What i want is this:
wind-speed-frequency-distribution-diagram

2 scales but the zero must be on the same line.
Is it possible on this library?

I dont want to make max and min the same because that would be the same as having just one yAxis.

@liuxuan30
Copy link
Member

liuxuan30 commented Nov 18, 2017

set both yAxis.axisMinimum = 0

@TouchFriend
Copy link

My solution is to set axisMinimum and axisMaximum, keep the zero lines in the middle.

/// 更新左右轴的值范围
/// @param chartView BarLineChartViewBase示例

  • (void)updateLeftRightAxisRange:(BarLineChartViewBase *)chartView {
    ChartData *chartData = chartView.data;
    CGFloat leftAxisMax = [chartData getYMaxWithAxis:AxisDependencyLeft];
    CGFloat leftAxisMin = [chartData getYMinWithAxis:AxisDependencyLeft];
    CGFloat rightAxisMax = [chartData getYMaxWithAxis:AxisDependencyRight];
    CGFloat rightAxisMin = [chartData getYMinWithAxis:AxisDependencyRight];
    // 左右两轴的最小值是否都大于等于0
    BOOL biggerZeroFlag = leftAxisMin >= 0 && rightAxisMin >= 0;
    // 设置左轴值范围
    CGFloat leftAxisAbsoluteMax = MAX(fabs(leftAxisMax), fabs(leftAxisMin));
    leftAxisMax = leftAxisAbsoluteMax;
    leftAxisMin = biggerZeroFlag ? 0 : -leftAxisMax;
    [self updateYAxisRange:chartView.leftAxis dataMin:leftAxisMin dataMax:leftAxisMax];
    // 设置右轴值范围
    CGFloat rightAxisAbsoluteMax = MAX(fabs(rightAxisMax), fabs(rightAxisMin));
    rightAxisMax = rightAxisAbsoluteMax;
    rightAxisMin = biggerZeroFlag ? 0 : -rightAxisMax;
    [self updateYAxisRange:chartView.rightAxis dataMin:rightAxisMin dataMax:rightAxisMax];
    }

/// 更新y轴值范围
/// @param yAxis y轴
/// @param dataMin 数据最小值
/// @param dataMax 数据最大值

  • (void)updateYAxisRange:(ChartYAxis *)yAxis dataMin:(CGFloat)dataMin dataMax:(CGFloat)dataMax {
    CGFloat range = fabs(dataMax - dataMin);
    CGFloat topSpace = range * yAxis.spaceTop;
    yAxis.axisMaximum = dataMax + topSpace;
    CGFloat bottomSpace = range * yAxis.spaceBottom;
    // 左右轴最小值都大于0,才设置最小值为0
    yAxis.axisMinimum = dataMin == 0 ? 0 : MIN(dataMin - bottomSpace, 0);
    }

All values are greater than 0:
QQ20201214-180241@2x

There are values less than 0:
QQ20201214-180319@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants