Ninja Charts- WordPress Charts and Graphs Shortcode

Below, you’ll find a detailed guide on how to add the Ninja Charts- WordPress Charts and Graphs Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Ninja Charts- WordPress Charts and Graphs Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Ninja Charts- WordPress Charts and Graphs Plugin and the shortcodes it provides:

✩✩✩✩✩ () Active Installs: + Tested with: PHP Version:
Included Shortcodes:

Ninja Charts- WordPress Charts and Graphs [ninja_charts] Shortcode

The Ninja Charts shortcode is designed to generate and display charts based on the ‘id’ attribute. It fetches the chart data from the NinjaCharts database using the specified ‘id’. In the event an invalid ‘id’ is provided, it returns an error message “Invalid ShortCode…!”. This shortcode is highly useful for rendering custom charts on your WordPress site.

Shortcode: [ninja_charts]

Parameters

Here is a list of all possible ninja_charts shortcode parameters and attributes:

  • id – The unique identifier for the specific chart you want to display.

Examples and Usage

Basic Example – Show a Ninja Chart using its ID

[ninja_charts id=1 /]

With the above shortcode, you can display a Ninja Chart on your WordPress site. You just need to replace ‘1’ with the ID of the Ninja Chart you want to display.

Advanced Examples

Unfortunately, the given code only supports one attribute, ‘id’. Therefore, any advanced examples would still only use this attribute. However, you can use multiple instances of the shortcode to display more than one chart.

Displaying two Ninja Charts by their IDs

[ninja_charts id=1 /]
[ninja_charts id=2 /]

With the above code, you can display two different Ninja Charts. The first shortcode displays the Ninja Chart with an ID of ‘1’, and the second shortcode displays the Ninja Chart with an ID of ‘2’. Just replace ‘1’ and ‘2’ with the IDs of the Ninja Charts you want to display.

PHP Function Code

In case you have difficulties debugging what causing issues with [ninja_charts] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('ninja_charts', [$this, 'makeShortCode']);

Shortcode PHP function:

function makeShortCode($atts = [], $content = null, $tag = '')
    {
        // normalize attribute keys, lowercase
        $atts = array_change_key_case((array)$atts, CASE_LOWER);
        // override default attributes with user attributes
        $wporg_atts = shortcode_atts([
            'id' => null,
        ], $atts, $tag);

        $id = Arr::get($wporg_atts, 'id');
        $ninjaCharts = NinjaCharts::find($id);
        if ($ninjaCharts) {
            $chart_data = Provider::get($ninjaCharts->data_source)->renderChart($ninjaCharts);
            return $this->renderView($ninjaCharts, $chart_data);
        } else {
            return __("Invalid ShortCode...!", 'ninja_charts');
        }
    }

Code file location:

ninja-charts/ninja-charts/app/Http/Controllers/ShortCodeController.php

Conclusion

Now that you’ve learned how to embed the Ninja Charts- WordPress Charts and Graphs Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *