How to hide value labels when zooming out in MPAndroidChart? - ios

I'm using Swift version of MPAndroidChart and trying to figure out how to hide value labels when a few of them are located too close together and start to overlap on a line chart.
Hiding all the value labels when zoomed out to a certain point works too(still don't know how to do it), but hiding only the ones that are overlapping would be the best.
I know that I can manually call setDrawValues = false, but I want it to be called automatically on zoom.
enter image description here

In Android, I use the same as iOS
1: Find the difference between 1st and 2nd index y-axis label(YAxisRenderer)
for (int i = from; i < to; i++)
{
String text = mYAxis.getFormattedLabel(i);
double value1=Double.parseDouble(mYAxis.getFormattedLabel(1).replace(",",""));
double value2=Double.parseDouble(mYAxis.getFormattedLabel(2).replace(",",""));
result=(value2-value1)-40;
//POPreferences.setDifference(String.valueOf(result));
c.drawText(text, fixedPosition, positions[i * 2 + 1] + offset, mAxisLabelPaint);
}
2: Use result variable when text draws on bar or line(BarChartRenderer.java):
if (result >= vals[k / 2])
{
drawValue(c,dataSet.getValueFormatter(),vals[k / 2], entry, i, x, y,color);
}

Related

Is there a way to have google sheets read imperial height as a number?

I'm making a sheet with details about a bunch of fictional characters, and one column I want to have is their height. I would also really like to use Conditional Formatting with a Color Scale to color-code the tallest and shortest characters, and everything in between.
Unfortunately, I live in the US, and am used to height expressed in feet and inches (e.g. 5'10''), which Google Sheets of course does not recognize as a number. Is there any way to remedy this, besides writing everything in terms of just inches (e.g. 60), such that I could apply conditional formatting directly to the column?
I've tried different formats (e.g. 5'10), and I considered having a hidden column with just the inch value and have conditional formatting work off of that row (doesn't work with Color Scale as far as I can tell, since you can't input a custom formula). One thought I had is somehow formatting things as an improper fraction with a denominator of 12, but hiding the denominator? But I have no idea how that would work. I've Googled as best I can, but I haven't found anything (everything's just about changing row height, which makes sense in hindsight).
I understand that you have two goals in mind. First of all, you should decide which unit length to use for managing heights. I have chosen inches, but you could work with feet if you need. This will simplify the scenario and will allow you to work easily with the data, but you could always create a function that translates inches to the foot/inches combo in order to show the data to a third party. This is the example table that I will use:
And this is my code, I will explain it at the bottom:
function main() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
data = sortTable(data);
sheet.getDataRange().setValues(data);
for (var i = 1; i < data.length; i++) {
data[i][2] = gradient(data.length, i);
}
for (var i = 1; i < data.length; i++) {
sheet.getRange(i, 2).setBackground("#" + data[i][2][0] + data[i][2][1] +
data[i][2][2]);
}
}
function sortTable(data) {
data.sort(function(a, b) {
return b[1] - a[1];
})
return data;
}
function gradient(arraySize, position) {
var relativePosition = position / arraySize;
var topColor = [parseInt("00", 16), parseInt("7A", 16), parseInt("33",
16)]; // Green
var bottomColor = [parseInt("FF", 16), parseInt("FF", 16), parseInt("FF",
16)]; // White
var positionColor = [0, 0, 0];
for (var i = 0; i < 3; i++) {
positionColor[i] = Math.floor(topColor[i] * (1 - relativePosition) +
bottomColor[i] * relativePosition).toString(16);
}
return positionColor;
}
First of all you have to read the data with a combination of getValues()/setValues(), and once you do that you can sort the table based on height so you can create the gradient later. Please notice how I separated the sorting function for better clarity.
After that you need the gradient color for setBackground(). To do so I developed a simple linear gradient function that calculates the RGB code from the top to the bottom. In my example the gradient fades from green to white, but you can change it. I also separated the gradient script into its own function. At this point you already have the sorted table and its gradient colors, so you only have to use setValues() and you are done. Feel free to leave any comment if you have doubts about this approach. This would be the final result:
UPDATE
Based in your comments I get that you need an imperial height format. For that case, you could use =INT(B2)&"' "&TRIM(TEXT(ROUND(MOD(B2,1)*12*16,0)/16,"# ??/??")&"""") (assuming that B2 contains the height). This approach will use Sheets Formulas to calculate the remainder part of the height, and its expression as an irreducible fraction. This is the final result:

How to check if element is visible in viewport?

If I am at the bottom of the page how can I check if some element is visible if that element is for example at the top of the page.
I need to write a test where I scroll down to the bottom of the page and when I click on div it moves me back to the top of the page. Next step is to check if I am at that position.
My idea was that I just need to check if that element is visible but it turns out that it is not that easy to do. Any suggestions?
.isDisplayed() and .isFocused() is not what I need.
Edit:
I had an idea and I need two methods for it.
First one is to get y coordinate of the element (we need only y), but the problem is that getY() returns number relative to your current position, so I need something that will move me to that element and then get y coordinate. So my method looked something like this:
int getYCoordinat() {
// first move to the element
interact {
moveToElement(element)
}
// get y coord
return element.getY()
}
second method looked something like this:
boolean isScrolledToElement(int yCoordinateOfElement) {
int y = element.getY()
return y == yCoordinateOfElement
}
}
So first I would call getYCoordinat(), store integer into yCoordinateOfElement.
Then I would click on div that moves me to the element and call isScrolledToElement(yCoordinateOfElement).
But it doesnt work because moveToElement moves you to that element only if the element is not visible. So if we stay where we are because moveToElement() didn't move us, because element is already visible, we get the wrong Y coordinate that won't be same as the one which we get when we click on div that moves us to the element.
Here is the only solution I could find. Basically you need to see if the pixel coordinate of the bottom-right corner of your element is within the window.
Taken from the link:
private boolean isWebElementVisible(WebElement w) {
Dimension weD = w.getSize();
Point weP = w.getLocation();
Dimension d = driver.manage().window().getSize();
int x = d.getWidth();
int y = d.getHeight();
int x2 = weD.getWidth() + weP.getX();
int y2 = weD.getHeight() + weP.getY();
return x2 <= x && y2 <= y;
}

Dealing with Bubble Chart overlap

We're working on a Highcharts Bubble Chart w/category axis and a lot of overlapping data points. Is there any way to control exactly how the bubbles are placed within the category? What we'd like to do is sort the bubbles ahead of time and then offset them from each other a bit. Some overlap is desirable so we'd prefer to not have to add additional categories to make sure there is no overlap.
Unfortunately we have no solution for controling positions of the bubbles. But you can request your propositin in our uservoice system http://highcharts.uservoice.com/
If you are using categories, your x values are the array index of the category for your value.
So to tweak the placement, you can tweak your x value by adding/subtracting small decimal amounts:
http://jsfiddle.net/yPLVP/10/
[-0.1,2,10]
Keeping in mind that +/- .5 is the center point between the categories - so for a value that falls within the 3rd category (x value 2), keep your x values between 1.55 and 2.45 (or so....)
I have the same problem, you can add this code:
function(chartObj) {
$.each(chartObj.series[0].data, function(i, point) {
var aux = 0;
if (i % 2 == 0)
aux = point.dataLabel.y + 6;
else
aux = point.dataLabel.y - 6;
point.dataLabel.attr({y:aux});
});
jsFiddle: http://jsfiddle.net/9m6wu/277/

Changing data in HighCharts series causes y-axis to blow up

I'm seeing some odd behavior in a Highcharts line chart. I have multiple series displayed, and need to let the user change what's called the "Map level" on the chart, which is a straight line across all time periods. Assuming that the correct series is
chart.series[i]
and that the new level that I want it set to is stored in var newMapLevel,
I'm changing that series' data like so:
data = chart.series[i].data;
for(j=0; j<data.length; j++){
data[j].y = newMapLevel;
}
chart.series[i].setData(data);
Calling this function has the desired effect UNLESS the new map level y_value is ONE greater than the highest y_value of all other series, in which case the y-axis scale blows up. In other words, if the y_axis scale is normally from 0 to 275,000, and the highest y_value of any of the other series is, say, 224,000, setting the new map level value to 224,001 causes the y_axis scale to become 0 to 27500M. Yes, that's 27.5 billion.
Might this be a bug in Highcharts? Or is there a better way to change the data in a series?
I've posted a fiddle: http://jsfiddle.net/earachefl/4FuNE/4/
I got my answer from the Highcharts forum:
http://highslide.com/forum/viewtopic.php?f=9&t=13594&p=59888#p59888
This doesn't work as smoothly as I'd like. When you go from 8 as your line to 2 as your line, the scale doesn't adjust back down until you enter another value. Perhaps it's a start in the right direction.
$(document).ready(function(){
$('#clickme').click(function(){
var newMapLevel = $('#newMAP').val();
if(newMapLevel){
for(i=0; i<chart.series.length; i++){
if(chart.series[i].name == 'Map Level'){
data = chart.series[i].data;
for(j=0; j<data.length; j++){
data[j].y = newMapLevel;
}
// get the extremes
var extremes = chart.yAxis[0].getExtremes();
//alert("dataMin: " + extremes.dataMin);
//alert("dataMax: " + extremes.dataMax);
// define a max YAxis value to use when setting the extremes
var myYMax = extremes.dataMax;
if (newMapLevel >= myYMax) {
myYMax = Number(newMapLevel) + 1; // number conversion required
}
if (myYMax > chart.yAxis[0].max) {
alert('cabbbie');
myYMax = chart.yAxis[0].max + 1;
}
//alert("myYMax: " + myYMax);
chart.yAxis[0].setExtremes(extremes.dataMin, myYMax)
// finally, set the line data
chart.series[i].setData(data);
}
}
}
}); });

Large SWT Image is not printed properly

Hei, SWT Gurus, I have a pretty weird situation. The thing is, that I am trying to print gantt chart in my Eclipse RCP application. Gantt chart is quite long and sometimes high as well. Its dimensions are following: height=3008px (2 vertical pages), width > 20000px. My print area can fit something like 1400x2000 px. First of all, I am creating image out with my chart (image is OK, I can save it separately and see, that everything is there). However, in order to print it on the paper, I am printing it piece by piece (moving source X and Y positions respectively). This algorithm was working fine for some time, but now something strange happened:
When chart is not high enough and can fit on 1 vertical page, then it is printed normally, but when it is 2 vertical pages, then only second vertical page is printed and first one is left out. There are no errors, nor anything, that could help me. I thought, may be there is not enough heap memory, so I allocated -Xmx1014m space to my application, but it didn't helped. So, I am really lost, and can not find any solution or even an explanation to this problem. I was trying to simply print image by gc.drwImage(image, x, y), but is also printed me only the second half of it. I am also printing some text after every try of printing an image, and it is printed
The code, that is responsible for printing an image is following:
for (int verticalPageNumber = 0; verticalPageNumber <= pageCount.vGanttChartPagesCount; verticalPageNumber++) {
// horizontal position needs to be reset to 0 before printing next bunch of horizontal pages
int imgPieceSrcX = 0;
for (int horizontalPageNumber = 0; horizontalPageNumber <= pageCount.hGanttChartPagesCount; horizontalPageNumber++) {
// Calculate bounds for the next page
final Rectangle printBounds = PrintingUtils.calculatePrintBounds(printerClientArea, scaleFactor, verticalPageNumber, horizontalPageNumber);
if (shouldPrint(printer.getPrinterData(), currentPageNr)
&& nextPageHasSomethingToPrint(imgPieceSrcX, imgPieceSrcY, totalGanttChartArea.width, totalGanttChartArea.height)) {
printer.startPage();
final Transform printerTransform = PrintingUtils.setUpTransform(printer, printerClientArea, scaleFactor, gc, printBounds);
printHeader(gc, currentPageNr, printBounds);
imgPieceSrcY = printBounds.y;
final int imgPieceSrcHeight =
imgPieceSrcY + printBounds.height < ganttChartImage.getBounds().height ? printBounds.height : ganttChartImage.getBounds().height
- imgPieceSrcY;
final int imgPieceSrcWidth =
imgPieceSrcX + printBounds.width < ganttChartImage.getBounds().width ? printBounds.width : ganttChartImage.getBounds().width
- imgPieceSrcX;
if (imgPieceSrcHeight > 0 && imgPieceSrcWidth > 0) {
// gantt chart is printed as image, piece by piece
gc.drawImage(ganttChartImage,
imgPieceSrcX, imgPieceSrcY,
imgPieceSrcWidth, imgPieceSrcHeight,
printBounds.x, printBounds.y,
imgPieceSrcWidth, imgPieceSrcHeight); // destination width and height equal to source width and height to prevent
// stretching/shrinking of image
// move x and y to print next image piece
gc.drawText("Text " + currentPageNr, imgPieceSrcX, imgPieceSrcY);
imgPieceSrcX += printBounds.width;
}
currentPageNr++;
printer.endPage();
printerTransform.dispose();
}
}
Thanks in advance.

Resources