Comments (36)
I just ran into this as well. It's too bad this feature request has been open for about 4 years now. To implement it I wrote my own custom y-axis renderer. To accomplish such a title on the left axis you can do something like this:
In your chart configuration:
leftAxis.xOffset = 45
leftYAxisRenderer = MyYAxisRenderer(viewPortHandler: viewPortHandler, yAxis: leftAxis, transformer: getTransformer(forAxis: .left))
And create your custom renderer:
class MyYAxisRenderer: YAxisRenderer {
private static let titleLabelPadding: CGFloat = 20
/**
Unfortunately iOS Charts has marked many of its methods with internal visibily
so they cannot be customized. Instead you often need to re-implement logic from
the charting framework.
*/
override func renderAxisLabels(context: CGContext) {
// Render the y-labels.
super.renderAxisLabels(context: context)
// Render the y-axis title using our custom renderer.
renderTitle(title: "Lorem ipsum dolor sit amet", inContext: context, x: MyYAxisRenderer.titleLabelPadding)
}
}
// MARK: Y-Axis titles.
private extension MyYAxisRenderer {
func renderTitle(title: String, inContext context: CGContext, x: CGFloat) {
guard let yAxis = self.axis as? YAxis else { return }
let attributes: [NSAttributedString.Key: Any] = [
.font: yAxis.labelFont,
.foregroundColor: yAxis.labelTextColor
]
// Determine the chart title's y-position.
let titleSize = title.size(withAttributes: attributes)
let verticalTitleSize = CGSize(width: titleSize.height, height: titleSize.width)
let point = CGPoint(x: x, y: (viewPortHandler.chartHeight - verticalTitleSize.height) / 2)
// Render the chart title.
ChartUtils.drawText(context: context,
text: title,
point: point,
attributes: attributes,
anchor: .zero,
angleRadians: .pi / -2)
}
}
This example will render the y-axis title with a left offset / padding of 20px, and the y-axis labels with a left offset of 45px.
I hope this helps some people...
from charts.
I've seen a lot of talk on adding axis titles on multiple threads, but still haven't found a code example of how to add them. I'm using Swift 4.2. My chart is working great other than I can't get axis titles to show up. Does anyone have a code example?
from charts.
+1, This would be awesome and an important feature - we're all taught since grade school to title our axes! :)
from charts.
@UberJason I mean I subclassed the axis class and apply an axisName property and made toe rotation. Most customized work could use subclass + override to achieve.
I am on vacation.. so I may check my old code and file a PR for v3.
from charts.
+1 Would love to see this feature
from charts.
Trying a simpler workaround using SwiftUI:
#4405
from charts.
Everything that is supported in the Android version is supported here with the exact same API !
from charts.
@danielgindi I understand this and already read the docs but maybe I've missed something, do you know the answer to my question?
from charts.
Well currently it is not implemented - I currently am not planning on implementing, so you can do it :-)
Or you can ask for it on the MPAndroidChart repo and when it's done I'll pull it here too
from charts.
it is possible. I have done this before. just similar to rotating the x axis labels. The whole point is calculating the correct anchor point for rotation.
I could do a PR, but the question is, what I did is simply 90 rotation and centered in the middle of y axis with a simple text and text color, font. Once we officially support it, people would ask more, like different angle, position, multiple lines, etc... That's painful
from charts.
I don't quite follow your explanation, because there's no axis label property to apply rotation and centering to. Do you just mean you manually added a label to the chart view and manually applied some transforms to it?
Respectfully, I suggest that even basic axis labels would be a huge benefit, and you wouldn't be forced to support any more features than you want to (after all, you're currently not planning to support this one). Hope you'll reconsider!
from charts.
+1 Would love to see this feature
from charts.
+1 Would love to see this feature
from charts.
+1 Would love to see this feature
from charts.
is this implemented?
from charts.
+1 Would love to see this feature
from charts.
+1
from charts.
Is there a way to get a frame position that one could use to insert any custom UIView?
from charts.
+1 would love to see that feature
from charts.
is this implemented? i need this.
from charts.
There is a PR #2387 , but not reviewed yet.
from charts.
+1 would love to see that feature
from charts.
+1 would love to see that feature
from charts.
+1 would love to see this feature. .....
from charts.
+1 would love to see that feature
from charts.
+1 this feature would be great!!
from charts.
+1
from charts.
from charts.
+1 would love to see that feature
from charts.
+1 me too
from charts.
Could really use this feature....
from charts.
I do want this feature too.
from charts.
Waiting for it....
from charts.
Me too............
from charts.
I do want this feature too..
from charts.
I do want this feature too.. I'm tired for every update of the rewrite
from charts.
Related Issues (20)
- Bar Chart Value Labels missing HOT 6
- Crash when running on 10.13 HOT 1
- line break HOT 2
- Random crashes when Charts is being used in our app ($s6Charts16BarChartRenderer) HOT 2
- facing problem with PieChart.noDataText is not display HOT 1
- visionOS support
- Need Privacy Manifest HOT 4
- DGCharts.framework/Headers/DGCharts-Swift.h:4769:2: Unsupported Swift architecture HOT 1
- Documentation not found
- dyld[1596]: Library not loaded: /System/Library/Frameworks/Charts.framework/Charts HOT 1
- DGCharts Y-axis labels are not aligned to left
- LineChartRenderer.drawHighlighted crash HOT 2
- CandleStickChart auto scaling min/max is not working correctly
- HighLight bar disable unselected
- Candle Chart, Stick not visible as expected
- [Unknown process name] CGAffineTransformInvert: singular matrix
- No chart data available
- Not able to archive because 'No such file or directory 'PackageFrameworks/DGChartsDynamic.framework/DGChartsDynamic'' HOT 4
- Future of DGCharts? HOT 5
- Depreciated functions used in ChartColorTemplates HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from charts.