英文:
Does LineDataSet state gets altered by chart.setData(lineData) in MPAndroidChart?
问题
我遇到了一个奇怪的行为,我有一个唯一的 LineDataSet 实例,它在两个不同的图表中使用。
这个 LineDataSet 在进入任何一个图表之前获取了它的配置。
这两个图表的尺寸相似。
但是其中一个被 Android 框架重新调整大小。
这种调整大小发生在一个
在这种情况下,容器是一个 FrameLayout,它比原始图表的高度小 100dp,宽度是它的原始 match_parent 宽度的 0.85%。
情况如下...
当第一个接收 LineDataSet 的图表是重新调整大小/较小的图表时。
LineDataSet 的文本和线条大小...看起来正常...我会说...不大不小。
当这个相同的 dataSet 用于第二个较大的图表时,线条文本和线条大小与第一个图表完全相等...
现在,如果第一个接收 LineDataSet 的图表是较大的一个,线条和文本都看起来非常小。
在向这个图表设置 .setData() 后,我然后继续向第二个较小的图表设置 .setData()...线条看起来与第一个图表完全相同...每个文本和线条都非常小。
这个效果似乎也发生在 BEZIER_CURVE 选项的平滑度上,因为它看起来比 BEZIER 的平滑度更高。
供参考,这是当大图表首先提供数据时,20f .setValueTextSize(
看起来像这样(这是从重新调整大小的图表的角度来看的):
当重新调整大小的图表首先提供数据时,20f 的外观如下(这是从重新调整大小的图表的角度来看的):
这是大图表首先提供数据时大小为 9f 的文本(从大图表的角度来看):
当首先将 9f 提供给较小/调整大小的图表时...一切看起来都正常。
这也发生在不同的 Android 版本上。
这是 Android 的特性还是因为 DataSet 保留了一些大小参数?
英文:
I have a weird behavior happening to me, where I have a unique LineDataSet instance used across 2 different charts.
This LineDataSet obtains its configuration BEFORE entering ANY one chart.
Both charts are of similar dimensions.
BUT one of them is resized by the Android framework.
This resizing occurs the moment a <layout> View is inflated and added to a container of different size.
In this case the container is a FrameLayout that is 100dp smaller than the original chart height and 0.85% its original match_parent width.
This is what happens...
When the first chart to receive the LineDataSet is the resized/smaller one.
The LineDataSet text and line sizes... look normal... I'd say... not big not small.
When this same dataSet is used in the second bigger chart, the line text, and line sizes are exactly equal as the first chart...
NOW IF the first chart to receive the LineDataSet is the BIGGER one, BOTH, the line AND text look extremely small.
After having .setData() to this chart, I then proceed to .setData() to the second smaller chart... the line looks exactly the same as the first chart... with every text and line extremely small.
The effect seems to be happening also in the smoothness of the BEZIER_CURVE option, as it looks more acute than what the BEZIER looks.
For reference this is what 20f .setValueTextSize(
looks like when the big chart is fed first with the data (This is the perspective from the resized/smaller chart):
And this is how 20f looks when the resized/smaller chart is fed first (This is the perspective from the resized/smaller chart):
This is text of size 9f, when the big chart is fed first (This is the perspective from the big chart):
This is the small/resized chart when the BIG chart is fed first with text of 9f:
When 9f is fed to the smaller/resized chart first... everything looks fine.
This is happening across different Android versions also.
Is this an Android thing or is it because the DataSet is keeping some size parameter within itself?
答案1
得分: 2
问题出现是因为在图表初始化之前就构建了 LineDataSet,因此,名为 "mMetrics" 的私有静态字段,类型为 DisplayMetrics.class(这是一个帮助将浮点数转换为与设备显示度量匹配的值的类),没有被初始化。
我的解决方案是调用:
Utils.init(getApplication());
我确保在一个地方调用了这个方法。
英文:
The issue arised because the LineDataSet was being built before the Chart was even initialized, as such, a private static field called "mMetrics" of type DisplayMetrics.class. (a class that helps convert the float into a value that matches the device display metrics), was not being initialized.
My solution was to call:
Utils.init(getApplication());
In a place I was sure to be called once.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论