{"id":18225,"date":"2015-11-12T05:01:17","date_gmt":"2015-11-11T19:01:17","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=18225"},"modified":"2015-11-12T18:26:57","modified_gmt":"2015-11-12T08:26:57","slug":"google-chart-linear-trendline-selection-event-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/google-chart-linear-trendline-selection-event-tutorial\/","title":{"rendered":"Google Chart Linear Trendline Selection Event Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/moving_lineartrendline.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Google Chart Linear Trendline Selection Event Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/LinearTrendline\/moving_linear_trendline.jpg\" title=\"Google Chart Linear Trendline Selection Event Tutorial\"  style=\"float:left;\"  \/><\/a><p class=\"wp-caption-text\">Google Chart Linear Trendline Selection Event Tutorial<\/p><\/div>\n<p>Mathematicians, scientists and engineers (like Land Surveyors), at the very least, know about error tolerance, and modelling data to a linear equation should they think the thing being measured should be linear by nature.  Errors in measurement are rarely avoided, so the concept of a &#8220;line of best fit&#8221; in mathematics helps model real life &#8220;flawed&#8221; measurements to a close fitting mathematical model, that can then be used to interpolate, and extrapolate (within reason).  Alas, extrapolating without reason, by people with an agenda, often clouds the issue of presenting the data in that rigorous mathematical form, when there is this error tolerance involved.<\/p>\n<p>So that brings us to a really very useful Google Chart broadly categorized as Trendlines, and today we are going to just talk about Linear Trendlines, the data of which can be mathematically modelled by the equations &#8230;<\/p>\n<p><code><br \/>\ny = Gradient * x + Offset<br \/>\n<\/code><\/p>\n<p> &#8230; that is used by presenting (x,y) co-ordinate pairs, and it will work out the &#8220;line of best fit&#8221; for you &#8230; cute, huh?!<\/p>\n<p>Today&#8217;s tutorial uses these Google Chart Linear Trendline in an HTML iframe element to allow a &#8220;moving linear trendline&#8221; to be built up.  Just as with the &#8220;moving histogram&#8221; of <a target=_blank title='Survey Topic or Poll Histogram Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/survey-topic-or-poll-histogram-primer-tutorial\/'>Survey Topic or Poll Histogram Primer Tutorial<\/a> or the way by which you can calculate a <a target=_blank title='Moving average information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Moving_average'>&#8220;moving average&#8221;<\/a>, at any point in time, it is possible to show an interim (perhaps), and perhaps complete (but maybe not) snapshot of the situation &#8230; pretty dynamic, huh?!<\/p>\n<p>That approach may help you establish if your data is truly linear, by nature, or not, as you build it up, by seeing how neatly it gels with a linear &#8220;line of best fit&#8221;.  In fact, with today&#8217;s work, we introduce into the Google Chart Linear Trendline a Google Chart <a target=_blank title='Google Chart events' href='https:\/\/developers.google.com\/chart\/interactive\/docs\/events?hl=en'>&#8220;select&#8221;<\/a> event functionality, calculating that offset of any data point to the Google Chart Linear Trendline &#8220;line of best fit&#8221;.  And we use the code as of a couple of days back when were were calculating the distance of a data point from a polygon of an HTML area tag within an HTML map tag, when we presented <a target=_blank title='ESL Vocabulary Getting Warmer Image Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/esl-vocabulary-getting-warmer-image-tutorial\/'>ESL Vocabulary Getting Warmer Image Tutorial<\/a> &#8230; as per &#8230;<\/p>\n<ol>\n<li>the problem boils down to being about one 3 co-ordinate (x,y) set represented by our data point (x,y) of interest and any two (x,y) co-ordinate sets on the &#8220;line of best fit&#8221; &#8230;<\/li>\n<li>for this co-ordinate set consider the three points as a triangle and use <a target=_blank title=\"Heron's Formula\" href='http:\/\/www.mathopenref.com\/heronsformula.html'>Heron&#8217;s Formula<\/a> to calculate the triangle area<br \/>\n<code><br \/>\nfunction herons_formula_triangle_area(xx1, yy1, xx2, yy2, xx3, yy3) {<br \/>\n  var a=Math.sqrt((xx1 - xx2) * (xx1 - xx2) + (yy1 - yy2) * (yy1 - yy2));<br \/>\n  var b=Math.sqrt((xx3 - xx2) * (xx3 - xx2) + (yy3 - yy2) * (yy3 - yy2));<br \/>\n  var c=Math.sqrt((xx3 - xx1) * (xx3 - xx1) + (yy3 - yy1) * (yy3 - yy1));<br \/>\n  var s=eval((a + b + c) \/ 2.0);<br \/>\n  return Math.sqrt(s * (s - a) * (s - b) * (s - c));<br \/>\n}<br \/>\n<\/code> &#8230;<\/li>\n<li>the base value in &#8220;area=base x height \/ 2&#8221; (area of a triangle) can be calculated as the line segment length (square root of the x co-ordinate difference squared plus the y co-ordinate difference squared)<br \/>\n<code><br \/>\nfunction find_dist(x1, y1, x2, y2) {<br \/>\n  return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));<br \/>\n}<br \/>\n<\/code> &#8230;<\/li>\n<li>so height=area x 2 \/ base &#8230;<br \/>\n<code><br \/>\nfunction calcdist() {<br \/>\n  var area=0.0, base=0.0, height=0.0, smallesth=-1.0;<br \/>\n         area=herons_formula_triangle_area(x3, y3, x1, y1, x2, y2);<br \/>\n         base=find_dist(x1, y1, x2, y2);<br \/>\n         if (eval(base) > 0.001) {<br \/>\n         height=eval((2.0 * area) \/ base);<br \/>\n         }<br \/>\n         smallesth=height;<br \/>\n  return smallesth;<br \/>\n}<br \/>\n<\/code>\n<\/li>\n<li>and our shortest distance is this calculated offset above<\/li>\n<\/ol>\n<p>Link to Google Chart Tools &#8220;spiritual home&#8221; &#8230; <a target=_blank href='https:\/\/developers.google.com\/chart\/interactive\/docs\/index' title='Google Chart Tools provide a perfect way to visualize data on your website. From simple line charts to complex hierarchical tree maps, the chart galley provides a large number of well-designed chart types. Populating your data is easy using the provided client- and server-side tools.\n'>via Google<\/a>.<br \/>\nLink to Google Chart Tools Linear Trendline information &#8230; <a target=_blank href='https:\/\/developers.google.com\/chart\/interactive\/docs\/gallery\/trendlines' title='Google Linear Trendlines'>via Google<\/a>.<\/p>\n<p>Codewise that leaves us with our HTML\/Javascript supervisor web application you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/moving_lineartrendline.html_GETME\" title='moving_lineartrendline.html'>moving_lineartrendline.html<\/a> (with its <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/moving_lineartrendline.html\" title='click picture'>live run<\/a>) supervising the PHP of the Google Chart Linear Trendline called <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/LinearTrendline\/linear_trendline.php--GETME\" title='linear_trendline.php'>linear_trendline.php<\/a> (which can be run <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/LinearTrendline\/linear_trendline.php\" title='linear_trendline.php live run'>on its own<\/a> as you wish), and which changed for Google Chart &#8220;select&#8221; event functionality as per <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/PHP\/LinearTrendline\/linear_trendline.php--GETME\" title='linear_trendline.php'>this link<\/a>.<\/p>\n<p>This extra functionality, available via the suffix &#8220;&amp;onclick=y&#8221; applied to the Google Chart Linear Trendline title, flows on directly to the iPad iOS App we created and talked about, last, with <a target=_blank title='Xcode Swift iOS Application End Game Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/xcode-swift-ios-application-end-game-primer-tutorial\/'>Xcode Swift iOS Application End Game Primer Tutorial<\/a>.<\/p>\n<p>Today we use a colour scheme involving violet and yellow with the web application, having read they often work well together.  What do you think?<\/p>\n<hr>\n<p id='pjhgcltt'>Previous relevant <a target=_blank title='PHP\/Javascript\/HTML Google Chart Linear Trendline Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/phpjavascripthtml-google-chart-linear-trendline-tutorial\/'>PHP\/Javascript\/HTML Google Chart Linear Trendline Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/LinearTrendline\/linear_trendline.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"PHP\/Javascript\/HTML Google Chart Linear Trendline Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/LinearTrendline\/LinearTrendline.jpg\" title=\"PHP\/Javascript\/HTML Google Chart Linear Trendline Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">PHP\/Javascript\/HTML Google Chart Linear Trendline Tutorial<\/p><\/div>\n<p>Here is a tutorial that introduces you to Google Graphs API, or Google Chart Tools, and its Linear Trendline functionality.  For the data of picture above I give thanks to <a target=_blank title='Action Research Projects' href='http:\/\/actionresearchprojects.wikispaces.com\/file\/view\/Wiki03_LPB.pdf'>Action Research Projects<\/a>.<\/p>\n<blockquote><p>Google Chart Tools provide a perfect way to visualize data on your website. From simple line charts to complex hierarchical tree maps, the chart galley provides a large number of well-designed chart types. Populating your data is easy using the provided client- and server-side tools.<\/p><\/blockquote>\n<p>This Linear Trendline illustrates Linear Regression (or &#8220;Line of Best Fit&#8221;) used a lot in Mathematics, Statistics, Economics, Engineering (for example, Land Surveying) and the Sciences.  Read about it from <a target=_blank title='Linear Regression information from Wikipedia' href='http:\/\/en.wikipedia.org\/wiki\/Linear_regression'>Wikipedia<\/a> below:<\/p>\n<blockquote><p>In statistics, linear regression is an approach to modeling the relationship between a scalar dependent variable y and one or more explanatory variables denoted X. The case of one explanatory variable is called simple linear regression. For more than one explanatory variable, it is called multiple linear regression. (This term should be distinguished from multivariate linear regression, where multiple correlated dependent variables are predicted,[citation needed] rather than a single scalar variable.)<\/p>\n<p>In linear regression, data are modeled using linear predictor functions, and unknown model parameters are estimated from the data. Such models are called linear models. Most commonly, linear regression refers to a model in which the conditional mean of y given the value of X is an affine function of X. Less commonly, linear regression could refer to a model in which the median, or some other quantile of the conditional distribution of y given X is expressed as a linear function of X. Like all forms of regression analysis, linear regression focuses on the conditional probability distribution of y given X, rather than on the joint probability distribution of y and X, which is the domain of multivariate analysis.<\/p>\n<p>Linear regression was the first type of regression analysis to be studied rigorously, and to be used extensively in practical applications. This is because models which depend linearly on their unknown parameters are easier to fit than models which are non-linearly related to their parameters and because the statistical properties of the resulting estimators are easier to determine.<\/p>\n<p>Linear regression has many practical uses. Most applications fall into one of the following two broad categories:<\/p>\n<p>    If the goal is prediction, or forecasting, linear regression can be used to fit a predictive model to an observed data set of y and X values. After developing such a model, if an additional value of X is then given without its accompanying value of y, the fitted model can be used to make a prediction of the value of y.<br \/>\n    Given a variable y and a number of variables X1, &#8230;, Xp that may be related to y, linear regression analysis can be applied to quantify the strength of the relationship between y and the Xj, to assess which Xj may have no relationship with y at all, and to identify which subsets of the Xj contain redundant information about y.<\/p>\n<p>Linear regression models are often fitted using the least squares approach, but they may also be fitted in other ways, such as by minimizing the &#8220;lack of fit&#8221; in some other norm (as with least absolute deviations regression), or by minimizing a penalized version of the least squares loss function as in ridge regression. Conversely, the least squares approach can be used to fit models that are not linear models. Thus, although the terms &#8220;least squares&#8221; and &#8220;linear model&#8221; are closely linked, they are not synonymous.<\/p><\/blockquote>\n<p>Let&#8217;s see some  <a target=_blank title='click picture' href='http:\/\/www.rjmprogramming.com.au\/PHP\/LinearTrendline\/linear_trendline.php'>PHP<\/a> code in live action for this tutorial where you define your linear trendline characteristics and data.<\/p>\n<p>Link to Google Chart Tools &#8220;spiritual home&#8221; &#8230; <a target=_blank href='https:\/\/developers.google.com\/chart\/interactive\/docs\/index' title='Google Chart Tools provide a perfect way to visualize data on your website. From simple line charts to complex hierarchical tree maps, the chart galley provides a large number of well-designed chart types. Populating your data is easy using the provided client- and server-side tools.\n'>via Google<\/a>.<br \/>\nLink to Google Chart Tools Linear Trendline information &#8230; <a target=_blank href='https:\/\/developers.google.com\/chart\/interactive\/docs\/gallery\/trendlines' title='Google Linear Trendlines'>via Google<\/a>.<\/p>\n<p>Link to some downloadable PHP programming code &#8230; rename to <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/PHP\/LinearTrendline\/linear_trendline.php_GETME' title='Download me'>linear_trendline.php<\/a>.\n<\/p>\n<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d1809' onclick='var dv=document.getElementById(\"d1809\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?s=Google+Chart\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d1809' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d18225' onclick='var dv=document.getElementById(\"d18225\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/mathematics\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d18225' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Mathematicians, scientists and engineers (like Land Surveyors), at the very least, know about error tolerance, and modelling data to a linear equation should they think the thing being measured should be linear by nature. Errors in measurement are rarely avoided, &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/google-chart-linear-trendline-selection-event-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,37],"tags":[400,513,518,576,652,701,752,861,932,997,1100,1730,1319],"class_list":["post-18225","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-tutorials","tag-event","tag-google","tag-google-chart","tag-html","tag-javascript","tag-line-of-best-fit","tag-mathematics","tag-onclick","tag-php","tag-programming","tag-science","tag-trendline","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/18225"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=18225"}],"version-history":[{"count":9,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/18225\/revisions"}],"predecessor-version":[{"id":18241,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/18225\/revisions\/18241"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=18225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=18225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=18225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}