• Hello,

    I am trying use the plugin as a stock chart and pull the data from mysql. I read through the tutorial but it seems to differ a bit from the wp plugin.

    So far I have:

    1. Created a php file that queries the database and formats it in JSON:
      $prefix = '';
      echo "[\n";
      while ( $row = mysqli_fetch_assoc( $result ) ) {
        echo $prefix . " {\n";
        echo '  "category": "' . $row['day'] . '",' . "\n";
        echo '  "value1": ' . $row['cloud_index'] . '' . "\n";
        echo " }";
        $prefix = ",\n";
      }
      echo "\n]";
    2. Setup the plugin and formatted the existing code for a new stock chart.

    My problem is that I’m not sure how to incorporate the JSON output into the javascript code:

    • Does it go into function generateChartData()?
    • Should I be using the following function somewhere?
      `AmCharts.ready(function() {
      // load the data
      var chartData = AmCharts.loadJSON(‘data.php’);`
    • Do I need this function? AmCharts.loadJSON = function(url)

    Here is the code in WordPress:

    var %CHART%_1 = [];
    
    generateChartData();
    
    function generateChartData() {
    //THIS IS WHERE I THINK I NEED HELP
    	var firstDate = new Date();
    	var a1 = 
    
    		%CHART%_1.push({
    			date: newDate,
    			value: a1,
    
    		});
    
    	}
    }
    
    AmCharts.makeChart("%CHART%", {
    	type: "stock",
        "theme": "none",
        pathToImages: "https://www.amcharts.com/lib/3/images/",
    
    	dataSets: [{
    			title: "Cloud Index",
    			fieldMappings: [{
    				fromField: "value",
    				toField: "value"
    			}, {
    				fromField: "volume",
    				toField: "volume"
    			}],
    			dataProvider: %CHART%_1,
    			categoryField: "date"
    		},
    
    	],
    
    	panels: [{
    
    			showCategoryAxis: true,
    			title: "Value",
    			percentHeight: 70,
    
    			stockGraphs: [{
    				id: "g1",
    
    				valueField: "value",
    				comparable: true,
    				compareField: "value",
    				balloonText: "<a href="https://codex.www.ads-software.com/title">title</a>:<b><a href="https://codex.www.ads-software.com/value">value</a></b>",
    				compareGraphBalloonText: "<a href="https://codex.www.ads-software.com/title">title</a>:<b><a href="https://codex.www.ads-software.com/value">value</a></b>"
    			}],
    
    			stockLegend: {
    				periodValueTextComparing: "[[percents.value.close]]%",
    				periodValueTextRegular: "[[value.close]]"
    			}
    		}
    
    	],
    
    	chartScrollbarSettings: {
    		graph: "g1"
    	},
    
    	chartCursorSettings: {
    		valueBalloonsEnabled: true
    	},
    
    	periodSelector: {
    		position: "left",
    		periods: [{
    			period: "MM",
    			selected: true,
    			count: 1,
    			label: "1 month"
    		}, {
    			period: "YYYY",
    			count: 1,
    			label: "1 year"
    		}, {
    			period: "YTD",
    			label: "YTD"
    		}, {
    			period: "MAX",
    			label: "MAX"
    		}]
    	},
    
    	dataSetSelector: {
    		position: "left"
    	}
    });

    Thanks.

    https://www.ads-software.com/plugins/amcharts-charts-and-maps/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author martynasma

    (@martynasma)

    Hi there,

    You don’t need generate generateChartData function. You’re not generating the data but rather loading it.

    You will definitely need loadJSON function, though. Put it anywhere in the JavaScript block. Then add it’s call within AmCharts.ready() enclosure.

    The final code might look something like this:

    AmCharts.loadJSON = function(url) {
      // create the request
      if (window.XMLHttpRequest) {
        // IE7+, Firefox, Chrome, Opera, Safari
        var request = new XMLHttpRequest();
      } else {
        // code for IE6, IE5
        var request = new ActiveXObject('Microsoft.XMLHTTP');
      }
    
      // load it
      // the last "false" parameter ensures that our code will wait before the
      // data is loaded
      request.open('GET', url, false);
      request.send();
    
      // parse adn return the output
      return eval(request.responseText);
    };
    
    var chartData = AmCharts.loadJSON('data.php');
    var %CHART% = AmCharts.makeChart("%CHART%", {
    	type: "stock",
        "theme": "none",
        pathToImages: "https://www.amcharts.com/lib/3/images/",
    
    	dataSets: [{
    			title: "Cloud Index",
    			fieldMappings: [{
    				fromField: "value",
    				toField: "value"
    			}, {
    				fromField: "volume",
    				toField: "volume"
    			}],
    			dataProvider: chartData,
    			categoryField: "date"
    		},
    
    	],
    
    	panels: [{
    
    			showCategoryAxis: true,
    			title: "Value",
    			percentHeight: 70,
    
    			stockGraphs: [{
    				id: "g1",
    
    				valueField: "value",
    				comparable: true,
    				compareField: "value",
    				balloonText: "<a href="https://codex.www.ads-software.com/title">title</a>:<b><a href="https://codex.www.ads-software.com/value">value</a></b>",
    				compareGraphBalloonText: "<a href="https://codex.www.ads-software.com/title">title</a>:<b><a href="https://codex.www.ads-software.com/value">value</a></b>"
    			}],
    
    			stockLegend: {
    				periodValueTextComparing: "[[percents.value.close]]%",
    				periodValueTextRegular: "[[value.close]]"
    			}
    		}
    
    	],
    
    	chartScrollbarSettings: {
    		graph: "g1"
    	},
    
    	chartCursorSettings: {
    		valueBalloonsEnabled: true
    	},
    
    	periodSelector: {
    		position: "left",
    		periods: [{
    			period: "MM",
    			selected: true,
    			count: 1,
    			label: "1 month"
    		}, {
    			period: "YYYY",
    			count: 1,
    			label: "1 year"
    		}, {
    			period: "YTD",
    			label: "YTD"
    		}, {
    			period: "MAX",
    			label: "MAX"
    		}]
    	},
    
    	dataSetSelector: {
    		position: "left"
    	}
    });
    Mariella2610

    (@mariella2610)

    Hello and thank you for this great plugin and for the support!
    I am also trying to fetch data located on a mysql database.
    I wonder, where do I need to upload the ‘data.php’ file? Is it in the amCharts plugin folder or in my theme folder, or somewhere else maybe?
    Thank you!

    Plugin Author martynasma

    (@martynasma)

    The location of the data.php file is not important. I suggest you put it into some other folder than amcharts code. It can reside in amcharts folder but it will be safer somewhere else in case you want to replace the whole amcharts folder while updating the version.

    Mariella2610

    (@mariella2610)

    Ok, great, thanks a lot for your reply!
    Then the path to the data.php file should be the path from the amcharts folder to the path chosen for the data.php file, is that right? In other words, if I leave the data.php file in the amcharts main folder, the path to the data.php file would simply be ‘data.php’ but if I put this file in an other folder, it would be for example ‘../example/data.php’?
    Best regards,

    Plugin Author martynasma

    (@martynasma)

    No. It will be relevant to the web page you are showing the chart on. So to be perfectly on the safe side you probably want to use absolute URLs, be it withing amcharts or some other folder. I.e.:

    ‘/data/data.php’

    or

    ‘/amcharts/data.php’

    Makes sense?

    Mariella2610

    (@mariella2610)

    Ok, I understand better now. Yes it makes sense, thanks again for your help!

    Plugin Author martynasma

    (@martynasma)

    Great.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WP Plugin Stock Chart & mysql Database’ is closed to new replies.