Amcharts rendering data incorrectly - ruby-on-rails

I have a setup where I am using amcharts that is feed data via appendData from an AJAX call. The call goes to a URL which simply renders the Time.now as the X and 8 lines using the function 2cos(x/2)+2ln (ln is the line number). AJAX request is made every 1 second.
The backend is always correct and always returns a single point, unless it is a duplicate X in which it throws an error. The error causes not to complete and therefore not call appendData.
Anybody have any idea what is going wrong with amcharts? It seems to be an issue only with appendData (which I need to simulate a sliding window).
The Javascript code is below. It assumes that the page creates a line chart with 8 points graphs and passes it to setup_chart_loader. Netcordia.rapid_poller.updateChart is used to update the chart using the Ajax request
Ext.ns("Netcordia.rapid_poller");
Netcordia.rapid_poller.refresh_rate = 1; //seconds
Netcordia.rapid_poller.pause = false; //causes the AJAX to suspend
Netcordia.rapid_poller.chart = null;
Netcordia.rapid_poller.stop = false;
/* This function does everything that is required to get the chart data correct */
Netcordia.rapid_poller.setup_chart_loader = function(chart){
assert(Netcordia.rapid_poller.displaySizeInMinutes,"No display size");
assert(Netcordia.rapid_poller.delta_url, "Data URL is empty");
assert(Netcordia.rapid_poller.delta_params, "No Data params");
if(typeof(chart) !== 'object'){
chart = document.getElementById(chart);
}
Netcordia.rapid_poller.chart = chart;
// 5 seconds raw polling
var maxPoints = Netcordia.rapid_poller.displaySizeInMinutes * 60 / 5;
var count = 0;
var lastUpdate = '';
debug("max number of points: "+maxPoints);
debug('creating updateChart function');
Netcordia.rapid_poller.updateChart = function(){
debug("Sending Data request");
var params = {last: lastUpdate, max: 1}; //maxPoints};
//I have to do this otherwise amcharts get a lot of data and only renders
// one item, then the counts is off
if(lastUpdate === ''){params['max'] = maxPoints;}
if (Netcordia.rapid_poller.pause){
alert("pausing");
params['historical'] = 1;
params['max'] = maxPoints;
}
Ext.apply(params, Netcordia.rapid_poller.delta_params);
//this might need to be moved to within the Ajax request
// incase things start piling up
if(!Netcordia.rapid_poller.stop){
setTimeout(Netcordia.rapid_poller.updateChart,1000*Netcordia.rapid_poller.refresh_rate);
} else {
debug("skipping next poll");
return;
}
Ext.Ajax.request({
url: Netcordia.rapid_poller.delta_url,
baseParams: Netcordia.rapid_poller.delta_params,
params: params,
success: function(response){
//if(Netcordia.rapid_poller.pause){
// debug("Data stopped");
// return;
//}
var json = Ext.util.JSON.decode(response.responseText);
lastUpdate = json.lastUpdate;
if( json.count === 0 ){
debug("no data to append");
return;
}
debug("appending "+json.count);
var remove = (count + json.count) - maxPoints;
if(remove <= 0){ remove = 0; }
count += json.count;
if(count > maxPoints){ count = maxPoints; }
debug("removing "+remove);
debug("count: "+count);
if(Netcordia.rapid_poller.pause){
alert("Pausing for historical");
//append a zero point and delete the existing data
// amcharts can leak extra points onto the screen so deleting
// twice the number is
chart.appendData("00:00:00;0;0;0;0;0;0;0;0",(count*2).toString());
count = json.count;
remove = 1;
Netcordia.rapid_poller.stop = true;
}
chart.appendData(json.lines.toString(),remove.toString());
}
});
};
};
The rails code that returns the data is as follows:
def get_delta
max = 1
begin
current = Time.parse(params[:last])
rescue
current = Time.now
end
if params[:historical]
max = params[:max].to_i || 10
current = Time.at(current.to_i - (max/2))
end
logger.info(current.to_i)
logger.info(max)
n = current.to_i
m = n+max-1
data = (n..m).collect do |x|
logger.info "For Point: #{x}"
point = Math.cos(x/2)
data = [Time.at(x).strftime("%H:%M:%S")]
for i in (1..8)
data.push(2*point+(2*i));
end
data.join(";")
end
render :json => {count: data.size, lastUpdate: Time.now.strftime('%Y-%m-%d %H:%M:%S'), lines: data.join("\n")}
end

Seems to be a bug in Amcharts itself.
Forum Post has the developer's answer.

Related

Why is my binary search implementation returning -1?

This is my main.dart:
import 'edgecases.dart';
main () {
var card = edgecases(0)['input']['cards'];
var query = edgecases(0)['input']['query'];
var result = locate_card(edgecases(0)['input']['cards'], edgecases(0)['input']['query']);
var output = edgecases(0)['output'];
print("Cards:- $card");
print("Query:- $query");
print("Output:- $result");
print("Actual answer:- $output");
}
And this is my edgecases.dart:
edgecases ([edgecasenumber = null]) { //You may make it required, I provided a null as default to check if my syntax is going right.
List tests = [];
var edge1 = {'input': {
'cards': [13, 11, 10, 7, 4, 3, 1, 0],
'query': 1
}, 'output': 6};
tests.addAll([edge1]);
if (edgecasenumber == null){ // This if is useless here so you may
return 'Null type object coud not be found.';
} else {
return tests.elementAt(edgecasenumber); // Indexing in dart also starts with 0.
}
}
locate_card (List cards, int query){
int lo = 0;
int hi = cards.length - 1;
print('$lo $hi');
while (lo <= hi) {
//print('hello'); Uncomment to see if it is entering the loop
var mid = (lo + hi) ~/ 2;
var mid_number = cards[mid];
print("lo:$lo ,hi:$hi, mid:$mid, mid_number:$mid_number");
if (mid_number == query){
return mid;
} else if (mid_number < query) {
hi = mid - 1;
} else if (mid_number > query) {
lo = mid + 1;
};
return -1; //taking about this line
};
}
[I have cut short the code here so you may find some things as unnecessary so just ignore it XD]
Actually I am trying to implement binary search here(I have previously successfully implemented it in python, I am implementing in dart to learn the language.)
On testing it with first edge case(that is on running the command dart main.dart), I found that it is returning the value -1 which was wrong, so I tried commenting the return -1; line in edgecases.dart file to see what happens as it was made to handle another edge case(edgecase if the list is empty, here I have removed that for simplicity). I am not able to understand why it is returning -1 if it gives the right value on commenting that line. Any possible explainations and solutions?
Thanks in advance!
You almost did it right. Just place the return -1; after the while loop's closing brace at the very end of locate_card.

Highcharts Showing Uncaught TypeError: Cannot read properties of undefined (reading 'chart')

I've had this HighCharts spider chart working fine for a while now, but we upgraded to the latest HighCharts code and I noticed that the mouseovers are no longer working. My PHP code looks like this:
// Create a new Highchart
$chart = new Highchart();
$chart->includeExtraScripts();
$chart->chart->renderTo = "control_maturity_spider_chart";
$chart->chart->polar = true;
$chart->chart->type = "line";
$chart->chart->width = 1000;
$chart->chart->height = 1000;
$chart->title->text = "Current vs Desired Maturity by Control Family";
$chart->title->x = -80;
$chart->pane->size = "80%";
$chart->xAxis->categories = $categories;
$chart->xAxis->tickmarkPlacement = "on";
$chart->xAxis->lineWidth = 0;
$chart->yAxis->gridLineInterpolation = "polygon";
$chart->yAxis->lineWidth = 0;
$chart->yAxis->min = 0;
$chart->yAxis->max = 5;
$chart->yAxis->tickInterval = 1;
$chart->tooltip->shared = true;
$chart->tooltip->pointFormat = '<span style="color:{series.color}">{series.name}: <b>{point.y}</b><br/>';
$chart->legend->align = "center";
$chart->legend->verticalAlign = "top";
$chart->legend->layout = "vertical";
// Draw the Current Maturity series
$chart->series[0]->name = $escaper->escapeHtml($lang['CurrentControlMaturity']);
$chart->series[0]->data = empty($categories_current_maturity_average) ? [] : $categories_current_maturity_average;
$chart->series[0]->pointPlacement = "on";
// Draw the Desired Maturity series
$chart->series[1]->name = $escaper->escapeHtml($lang['DesiredControlMaturity']);
$chart->series[1]->data = empty($categories_desired_maturity_average) ? [] : $categories_desired_maturity_average;
$chart->series[1]->pointPlacement = "on";
$chart->credits->enabled = false;
echo "<figure class=\"highcharts-figure\">\n";
echo " <div id=\"control_maturity_spider_chart\"></div>\n";
echo "</figure>\n";
echo "<script type=\"text/javascript\">";
echo $chart->render("control_maturity_spider_chart");
echo "</script>\n";
The actual chart renders just fine, but if you mouse over it, you just get this message in the javascript console over and over again:
HighCharts Error Message
If we comment out these two lines of code, the mouseover works:
$chart->tooltip->shared = true;
$chart->tooltip->pointFormat = '<span style="color:{series.color}">{series.name}: <b>{point.y}</b><br/>';
Any thoughts on what we are doing wrong here, or what changed, would be greatly appreciated. Thank you.
this is the bug which you can track here: https://github.com/highcharts/highcharts/issues/17472
As a temporary workaround, add the following wrap function to your code:
(function(H) {
const isObject = H.isObject;
H.Pointer.prototype.findNearestKDPoint = function(series, shared, e) {
var chart = this.chart;
var hoverPoint = chart.hoverPoint;
var tooltip = chart.tooltip;
if (hoverPoint &&
tooltip &&
tooltip.isStickyOnContact()) {
return hoverPoint;
}
var closest;
/** #private */
function sort(p1, p2) {
var isCloserX = p1.distX - p2.distX,
isCloser = p1.dist - p2.dist,
isAbove = ((p2.series.group && p2.series.group.zIndex) -
(p1.series.group && p1.series.group.zIndex));
var result;
// We have two points which are not in the same place on xAxis
// and shared tooltip:
if (isCloserX !== 0 && shared) { // #5721
result = isCloserX;
// Points are not exactly in the same place on x/yAxis:
} else if (isCloser !== 0) {
result = isCloser;
// The same xAxis and yAxis position, sort by z-index:
} else if (isAbove !== 0) {
result = isAbove;
// The same zIndex, sort by array index:
} else {
result =
p1.series.index > p2.series.index ?
-1 :
1;
}
return result;
}
series.forEach(function(s) {
var noSharedTooltip = s.noSharedTooltip && shared,
compareX = (!noSharedTooltip &&
s.options.findNearestPointBy.indexOf('y') < 0),
point = s.searchPoint.call(s.polar, e, compareX);
if ( // Check that we actually found a point on the series.
isObject(point, true) && point.series &&
// Use the new point if it is closer.
(!isObject(closest, true) ||
(sort(closest, point) > 0))) {
closest = point;
}
});
return closest;
};
}(Highcharts))
Demo:
https://jsfiddle.net/BlackLabel/8b2mhqf0/

Get count of characters for translation in Kentico Cloud

Is there a way to tell the count of characters of all text fields in some of our content items? We need to estimate a translation price for our content items.
You can use Delivery API to retrieve your items and run a quick javascript to count the characters for you. First, get all your items (or a subset, depending on what you need) with the call excluding all the modular content (linked items) like this:
https://deliver.kenticocloud.com/<projectid>/items?depth=0​​​​​​​
Then you can use browser console to run this piece of code:
var response = JSON.parse(document.getElementsByTagName("BODY")[0].textContent);
var noOfChars = 0;
for (var x = 0; x < response.items.length; x++) {
var p = response.items[x].elements;
for (var key in p) {
if (p[key].type=='rich_text' || p[key].type=='text') {
noOfChars += strip(p[key].value).length;
}
}
}
noOfChars;
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
And hit enter. This is what the result will look like:

Swarming in AS2

Hi I keep getting the error: expecting identifier before greater than.
on line 13.
Any help would be nice please and Thank you.
fly = function () {
this.animate = function() {
// Capture mouse positions and distance from mouse
this.targetX = _root._xmouse;
this.targetY = _root._ymouse;
this.distX = this.targetX-this.meX+this.flockX;
this.distY = this.targetY-this.meY+this.flockY;
//
if ((this.targetX == this.oldTargetX) && Math.random()>0.9) {
// add small scale random darting if mouse is still
this.flockX = (Math.random()*100)-50;
this.flockY = (Math.random()*100)-50;
} else if ((this.targetX<>this.oldTargetX) && Math.random()>0.8) {
// add large scale random darting if mouse is moving
this.flockX = (Math.random()*400)-200;
this.flockY = (Math.random()*400)-200;
}
// Apply inertia equation
this.meX = Math.round(this.meX+(this.distX)/20);
this.meY = Math.round(this.meY+(this.distY)/20);
// perform animation
this._x = this.meX;
this._y = this.meY;
// remember the current mouse pos so we can tell if
// it has moved next time around
this.oldTargetX = this.targetX;
};
this.initialize = function() {
this.targetX = 0;
this.targetY = 0;
this.distX = 0;
this.distY = 0;
this.meX = this._x;
this.meY = this._y;
this.oldTargetX = 0;
this.flockX = (Math.random()*200)-100;
this.flockY = (Math.random()*200)-100;
};
// set up onEnterFrame script to animate _parent...
this.initialize();
this.onEnterFrame = this.animate;
};
//
//
var i:Number = 0;
var bugClip:MovieClip;
for (i=0; i<30; i++) {
bugClip = this.attachMovie("bug", "bug"+i, i);
fly.apply(bugClip);
}
I don't know about Actionscript, but by looking at your code I would recomend doing like this:
randomValue = Math.random()
if ((this.targetX == this.oldTargetX) && randomValue>0.9) {
The <> operator for not equals has been deprecated since Flash Player 5 Doc reference here
You should use != for the same functionality.
Although i tested this on Flash Player 10.2 and it will still compile and run with no errors. I guess you are compiling to a later version.

Actionscript randomly distribute objects on stage

I'm trying to distribute 3 objects randomly on my stage but it's not working. My movie is 800x800.
function makeRock():void{
var tempRock:MovieClip;
for(var i:Number = 1; i < 3; i++){
tempRock = new Rock();
tempRock.x = Math.round(800);
tempRock.y = Math.round(-800);
addChild(tempRock);
}
}
What am I doing wrong?
Replace Math.round(800); with Math.random()*800;
function makeRock():void
{
var tempRock:MovieClip;
var i:uint = 0;
for(i; i < 3; i++)
{
tempRock = new Rock();
tempRock.x = Math.random()*800;
tempRock.y = Math.random()*800;
addChild(tempRock);
}
}
Math.round(800) is just returning 800.
Math.random() returns a random number between 0 and 1, which you can multiply by 800 to get a random result of 0-800. A good note to make is that Math.random() never actually returns 1.0. Just everything from 0 up to 1.
Further reading:
As a side note: this makes it simple to return a random element from an array; because you're never getting 1 you can cast the result of Math.random()*array.length to uint() and always be within the boundaries of the array length.
eg.
var ar:Array = [1,2,"hello",4,5,6,7,8,9,0];
var randomElement:Object = ar[uint(Math.random()*ar.length)];
trace(randomElement);

Resources