Skip to content

Commit

Permalink
Added StockChart Support
Browse files Browse the repository at this point in the history
  • Loading branch information
vishwas-r committed Aug 14, 2020
1 parent fa1e199 commit 2bd482f
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 43 deletions.
4 changes: 2 additions & 2 deletions LICENSE.txt → LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Vishwas.R
Copyright (c) 2020 Vishwas.R

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
# CanvasJS Charts for WordPress
# CanvasJS for WordPress

This plugin allows you to create and add interactive charts to your wordpress page and posts using CanvasJS library. It creates responsive high-performance charts that renders across devices including iPhone, iPad, Android, Mac & PCs.
This plugin allows you to create and add interactive Charts & StockCharts to your wordpress page and posts using CanvasJS library. It creates responsive high-performance Charts & StockCharts that renders across devices including iPhone, iPad, Android, Mac & PCs.

## CanvasJS
CanvasJS is built from ground up for high performance data visualization and can render millions of data points in a matter of milliseconds. CanvasJS is trusted by 500K+ users in 192 Countries. Customers include Microsoft, NASA, Apple, Intel, Boing, Samsung, BMW, Sony, HP etc
## CanvasJS Library
CanvasJS is built from ground up for high performance data visualization and can render millions of data points in a matter of milliseconds. CanvasJS is trusted by 500K+ users in 192 Countries. Customers include Microsoft, NASA, Apple, Intel, Boing, Samsung, BMW, Sony, HP, Bosch, Motorola, etc.

Note:
- This plugin requires you to have CanvasJS License. Please visit [CanvasJS](https://canvasjs.com/license/) for more info.

#### How to add CanvasJS to your WordPress Page / Post?
## CanvasJS Gallery / Demos
[CanvasJS Chart Gallery](https://canvasjs.com/javascript-charts/)
[CanvasJS StockChart Gallery](https://canvasjs.com/javascript-stockcharts/)

## How to add CanvasJS to your WordPress Page / Post?
#### CanvasJS Chart
- Add shortcode `[canvasjschart]`
- Pass chart-options to the chart as 'options'.
>[canvasjschart options="{title:{text: 'CanvasJS Column Chart'}, data: new Array({dataPoints: new Array({ label: 'apple', y: 10 },{ label: 'orange', y: 15 },{ label: 'banana', y: 25 },{ label: 'mango', y: 30 },{ label: 'grape', y: 28 })})}" style="width:100%;height:300px"]
#### CanvasJS StockChart
- Add shortcode `[canvasjsstockchart]`
- Pass chart-options to the chart as 'options'.
>[canvasjsstockchart options="{title:{text: 'CanvasJS StockChart'}, charts: new Array({data: new Array({dataPoints: new Array({ label: 'apple', y: 10 },{ label: 'orange', y: 15 },{ label: 'banana', y: 25 },{ label: 'mango', y: 30 },{ label: 'grape', y: 28 })})})}" style="width:100%;height:400px"]
Note:
- Don't add space within style. i.e. Use `style="width:100%;height:300px"` and avoid `style="width: 100%; height: 300px"`

#### Plugin Testing
- Last Tested with: WordPress 5.5
- Last Tested on: Aug 14, 2020

#### License
MIT
36 changes: 23 additions & 13 deletions admin/admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@
defined( 'ABSPATH' ) or exit;


function canvasjs_charts_register_settings() {
add_option( 'canvasjs_charts_url', 'https://canvasjs.com/assets/script/canvasjs.min.js');
register_setting( 'canvasjs_charts_options_group', 'canvasjs_charts_url', 'canvasjs_charts_callback' );
function canvasjs_chart_register_settings() {
add_option( 'canvasjs_chart_url', 'https://canvasjs.com/assets/script/canvasjs.min.js');
register_setting( 'canvasjs_chart_options_group', 'canvasjs_chart_url', 'canvasjs_chart_callback' );
}
add_action( 'admin_init', 'canvasjs_charts_register_settings' );
add_action( 'admin_init', 'canvasjs_chart_register_settings' );

function canvasjs_stockchart_register_settings() {
add_option( 'canvasjs_stockchart_url', 'https://canvasjs.com/assets/script/canvasjs.stock.min.js');
register_setting( 'canvasjs_stockchart_options_group', 'canvasjs_stockchart_url', 'canvasjs_stockchart_callback' );
}
add_action( 'admin_init', 'canvasjs_stockchart_register_settings' );


function canvasjs_charts_register_options_page() {
add_menu_page('CanvasJS Charts Settings', 'CanvasJS Charts', 'manage_options', 'canvasjscharts', 'canvasjs_charts_options_page', 'dashicons-chart-pie', 6);
function canvasjs_chart_register_options_page() {
add_menu_page('CanvasJS Settings', 'CanvasJS Charts & StockCharts', 'manage_options', 'canvasjscharts', 'canvasjs_settings_page', 'dashicons-chart-pie', 6);
}
add_action('admin_menu', 'canvasjs_charts_register_options_page');
add_action('admin_menu', 'canvasjs_chart_register_options_page');


function canvasjs_charts_options_page() {
function canvasjs_settings_page() {
?>
<div>
<?php screen_icon(); ?>
<h1>WP CanvasJS Charts</h1>
<h1>WP CanvasJS Charts & StockCharts</h1>
<p><a href="https://canvasjs.com">CanvasJS</a> is an HTML5 & JavaScript based Charting Library that runs on all modern devices including iPhone, Android, Desktops, etc. Charts are beautiful and API is very simple to use.</p>

<form method="post" action="options.php">
<?php settings_fields( 'canvasjs_charts_options_group' ); ?>
<?php settings_fields( 'canvasjs_chart_options_group' ); ?>
<?php settings_fields( 'canvasjs_stockchart_options_group' ); ?>
<h2>Settings:</h2>
<p>If you have commercial license, you can upload the commercial version of CanvasJS to your server and provide the link below</p>
<label for="canvasjs_charts_url">CanvasJS Script URL</label>
<input type="text" id="canvasjs_charts_url" style="width: 30em" name="canvasjs_charts_url" value="<?php echo get_option('canvasjs_charts_url'); ?>" />
<label for="canvasjs_chart_url">CanvasJS Charts Script URL</label>
<input type="text" id="canvasjs_chart_url" style="width: 30em" name="canvasjs_chart_url" value="<?php echo get_option('canvasjs_chart_url'); ?>" />

<label for="canvasjs_stockchart_url">CanvasJS StockCharts Script URL</label>
<input type="text" id="canvasjs_stockchart_url" style="width: 30em" name="canvasjs_stockchart_url" value="<?php echo get_option('canvasjs_stockchart_url'); ?>" />
<?php submit_button(); ?>
</form>
<p><h4>Note: You can replace the above URL with the self-hosted commercial version of CanvasJS. Please visit <a href="https://canvasjs.com/license/" target="_blank">CanvasJS.com</a> for more info.</h4></p>
Expand All @@ -38,7 +48,7 @@ function canvasjs_charts_options_page() {


function canvasjs_admin_page() {
echo '<h3>CanvasJS Charts</h3>';
echo '<h3>CanvasJS Charts & StockCharts</h3>';
?>
<div>
<div class="wrap">
Expand Down
File renamed without changes.
26 changes: 13 additions & 13 deletions charts/wp-canvasjs-chart.php → chart/wp-canvasjs-chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
/**
* canvasjschart shortcode
*
* @package WPCanvasJSCharts
* @package WPCanvasJS
* @since 1.0.0
*/

if (!function_exists('canvasjs_script')){
function add_canvasjs_script() {
$_url = get_option('canvasjs_charts_url');
wp_register_script('canvasjs_script', $_url, array('jquery'),'1.1', true);
wp_enqueue_script('canvasjs_script');
if (!function_exists('canvasjs_chart_script')){
function add_canvasjs_chart_script() {
$_url = get_option('canvasjs_chart_url');
wp_register_script('canvasjs_chart_script', $_url, array('jquery'),'1.1', true);
wp_enqueue_script('canvasjs_chart_script');
}
add_action('wp_enqueue_scripts', 'add_canvasjs_script');
add_action('wp_enqueue_scripts', 'add_canvasjs_chart_script');
}

if (!function_exists('canvasjs_chart_shortcode')){
Expand All @@ -30,19 +30,19 @@ function add_canvasjs_script() {
* @since 1.0.0
*/
function canvasjs_chart_shortcode($atts) {
STATIC $count = 0;
$containerID = "chartContainer".$count;
$chart = "chart".$count;
$count++;
STATIC $chartCount = 0;
$chartContainerID = "chartContainer".$chartCount;
$chart = "chart".$chartCount;
$chartCount++;

$chartOptions = $atts['options'];
$divStyle = $atts['style'];

//Safe in PHP 7.0.
$_chart .= '<div id='.$containerID.' style='.$divStyle.'></div>
$_chart .= '<div id='.$chartContainerID.' style='.$divStyle.'></div>
<script>
jQuery(document).ready(function( $ ){
var '.$chart.'= new CanvasJS.Chart('. $containerID .','. $chartOptions .');'.
var '.$chart.'= new CanvasJS.Chart('. $chartContainerID .','. $chartOptions .');'.
$chart.'.render();
});
</script>';
Expand Down
1 change: 1 addition & 0 deletions stockchart/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php // Silence is golden
53 changes: 53 additions & 0 deletions stockchart/wp-canvasjs-stockchart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* canvasjsstockchart shortcode
*
* @package WPCanvasJS
* @since 1.2.0
*/

if (!function_exists('canvasjs_stockchart_script')){
function add_canvasjs_stockchart_script() {
$_url = get_option('canvasjs_stockchart_url');
wp_register_script('canvasjs_stockchart_script', $_url, array('jquery'),'1.1', true);
wp_enqueue_script('canvasjs_stockchart_script');
}
add_action('wp_enqueue_scripts', 'add_canvasjs_stockchart_script');
}

if (!function_exists('canvasjs_stockchart_shortcode')){
// Add the action.
add_action('plugins_loaded', function() {
// Add the shortcode.
add_shortcode( 'canvasjsstockchart', 'canvasjs_stockchart_shortcode' );
});

/**
* Shortcode Function
*
* @param Attributes $atts
* @return string
* @since 1.2.0
*/
function canvasjs_stockchart_shortcode($atts) {
STATIC $stockChartCount = 0;
$stockChartContainerID = "stockChartContainer".$stockChartCount;
$stockChart = "stockChart".$stockChartCount;
$stockChartCount++;

$stockChartOptions = $atts['options'];
$divStyle = $atts['style'];

//Safe in PHP 7.0.
$_stockChart .= '<div id='.$stockChartContainerID.' style='.$divStyle.'></div>
<script>
jQuery(document).ready(function( $ ){
var '.$stockChart.'= new CanvasJS.StockChart('. $stockChartContainerID .','. $stockChartOptions .');'.
$stockChart.'.render();
});
</script>';

// Return the data.
return $_stockChart;
}
} // End if().
24 changes: 14 additions & 10 deletions wp-canvasjs-charts.php → wp-canvasjs.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php
/**
* Plugin Name: WP CanvasJS Charts
* Plugin URI: https://csultimates.com/
* Description: Add CanvasJS Charts to your WordPress Pages / Posts
* Version: 1.1.0
* Plugin Name: WP CanvasJS
* Plugin URI: https://vishwas.me/
* Description: Add CanvasJS Charts & StockCharts to your WordPress Pages / Posts
* Version: 1.2.0
* Author: Vishwas R
* Author URI: https://csultimates.com/
* Author URI: https://vishwas.me/
* License: MIT License
* License URI: https://opensource.org/licenses/MIT
*
* @link https://csultimates.com/
* @package WPCanvasJSCharts
* @link https://vishwas.me/
* @package WPCanvasJS
*/

// If this file is called directly, abort.
Expand All @@ -25,7 +25,7 @@
*/
// Plugin version.
if ( ! defined( 'PLUGIN_VERSION' ) ) {
define( 'PLUGIN_VERSION', '1.0.0' );
define( 'PLUGIN_VERSION', '1.2.0' );
}

if ( ! defined( 'PLUGIN_NAME' ) ) {
Expand All @@ -45,8 +45,12 @@
*
* @since 1.0.0
*/
if ( file_exists( PLUGIN_DIRECTORY . '/charts/wp-canvasjs-chart.php' ) ) {
require_once( PLUGIN_DIRECTORY . '/charts/wp-canvasjs-chart.php' );
if ( file_exists( PLUGIN_DIRECTORY . '/chart/wp-canvasjs-chart.php' ) ) {
require_once( PLUGIN_DIRECTORY . '/chart/wp-canvasjs-chart.php' );
}

if ( file_exists( PLUGIN_DIRECTORY . '/stockchart/wp-canvasjs-stockchart.php' ) ) {
require_once( PLUGIN_DIRECTORY . '/stockchart/wp-canvasjs-stockchart.php' );
}

if ( file_exists( PLUGIN_DIRECTORY . '/admin/admin-settings.php' ) ) {
Expand Down

0 comments on commit 2bd482f

Please sign in to comment.