Published on

Ultimate Guide: Make A Stock Chart with AlpineJS and ChartsJS With Tailwind CSS

Stock Chart with AlpineJS and ChartsJS

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Stock Chart with AlpineJS and ChartsJS ui component

Buy me a beer 🍺

Why use Tailwind CSS to build a Stock Chart with AlpineJS and ChartsJS ui component?

  • It can make the building process of Stock Chart with AlpineJS and ChartsJS ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Stock Chart with AlpineJS and ChartsJS component file.

The preview of Stock Chart with AlpineJS and ChartsJS ui component

Free download of the Stock Chart with AlpineJS and ChartsJS's source code

The source code of Stock Chart with AlpineJS and ChartsJS ui component

<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>

<div class="min-w-screen min-h-screen bg-gray-900 flex items-center justify-center px-5 py-5">
    <div class="rounded shadow-xl overflow-hidden w-full md:flex" style="max-width:900px" x-data="{stockTicker:stockTicker()}" x-init="stockTicker.renderChart()">
        <div class="flex w-full md:w-1/2 px-5 pb-4 pt-8 bg-indigo-500 text-white items-center">
            <canvas id="chart" class="w-full"></canvas>
        </div>
        <div class="flex w-full md:w-1/2 p-10 bg-gray-100 text-gray-600 items-center">
            <div class="w-full">
                <h3 class="text-lg font-semibold leading-tight text-gray-800" x-text="stockTicker.stockFullName"></h3>
                <h6 class="text-sm leading-tight mb-2"><span x-text="stockTicker.stockShortName"></span>&nbsp;&nbsp;-&nbsp;&nbsp;Aug 2nd 4:00pm AEST</h6>
                <div class="flex w-full items-end mb-6">
                    <span class="block leading-none text-3xl text-gray-800" x-text="stockTicker.price.current.toFixed(3)">0</span>
                    <span class="block leading-5 text-sm ml-4 text-green-500" x-text="`${stockTicker.price.high-stockTicker.price.low<0?'':''} ${(stockTicker.price.high-stockTicker.price.low).toFixed(3)} (${(((stockTicker.price.high/stockTicker.price.low)*100)-100).toFixed(3)}%)`"></span>
                </div>
                <div class="flex w-full text-xs">
                    <div class="flex w-5/12">
                        <div class="flex-1 pr-3 text-left font-semibold">Open</div>
                        <div class="flex-1 px-3 text-right" x-text="stockTicker.price.open.toFixed(3)">0</div>
                    </div>
                    <div class="flex w-7/12">
                        <div class="flex-1 px-3 text-left font-semibold">Market Cap</div>
                        <div class="flex-1 pl-3 text-right" x-text="stockTicker.price.cap.m_formatter()">0</div>
                    </div>
                </div>
                <div class="flex w-full text-xs">
                    <div class="flex w-5/12">
                        <div class="flex-1 pr-3 text-left font-semibold">High</div>
                        <div class="px-3 text-right" x-text="stockTicker.price.high.toFixed(3)">0</div>
                    </div>
                    <div class="flex w-7/12">
                        <div class="flex-1 px-3 text-left font-semibold">P/E ratio</div>
                        <div class="pl-3 text-right" x-text="stockTicker.price.ratio.toFixed(2)">0</div>
                    </div>
                </div>
                <div class="flex w-full text-xs">
                    <div class="flex w-5/12">
                        <div class="flex-1 pr-3 text-left font-semibold">Low</div>
                        <div class="px-3 text-right" x-text="stockTicker.price.low.toFixed(3)">0</div>
                    </div>
                    <div class="flex w-7/12">
                        <div class="flex-1 px-3 text-left font-semibold">Dividend yield</div>
                        <div class="pl-3 text-right" x-text="`${stockTicker.price.dividend}%`">0%</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="absolute bottom-0 pb-2 text-gray-700 text-xs w-full text-center">
  See more: <a href="https://codepen.io/ScottWindon" class="underline hover:text-gray-500" target="_blank">https://codepen.io/ScottWindon</a>
</div>

<script>
Number.prototype.m_formatter = function() {
    return this > 999999 ? (this / 1000000).toFixed(1) + 'M' : this
};
let stockTicker = function(){
    return {
        stockFullName: 'SW Limited.',
        stockShortName: 'ASX:SFW',
        price: {
            current: 2.320,
            open: 2.230,
            low: 2.215,
            high: 2.325,
            cap: 93765011,
            ratio: 20.10,
            dividend: 1.67
        },
        chartData: {
            labels: ['10:00','','','','12:00','','','','2:00','','','','4:00'],
            data: [2.23,2.215,2.22,2.25,2.245,2.27,2.28,2.29,2.3,2.29,2.325,2.325,2.32],
        },
        renderChart: function(){
            let c = false;

            Chart.helpers.each(Chart.instances, function(instance) {
                if (instance.chart.canvas.id == 'chart') {
                    c = instance;
                }
            });

            if(c) {
                c.destroy();
            }

            let ctx = document.getElementById('chart').getContext('2d');

            let chart = new Chart(ctx, {
                type: "line",
                data: {
                    labels: this.chartData.labels,
                    datasets: [
                        {
                            label: '',
                            backgroundColor: "rgba(255, 255, 255, 0.1)",
                            borderColor: "rgba(255, 255, 255, 1)",
                            pointBackgroundColor: "rgba(255, 255, 255, 1)",
                            data: this.chartData.data,
                        },
                    ],
                },
                layout: {
                    padding: {
                        right: 10
                    }
                },
                options: {
                    legend: {
                        display: false,
                    },
                    scales: {
                        yAxes: [{
                            ticks: {
                                fontColor: "rgba(255, 255, 255, 1)",
                            },
                            gridLines: {
                                display: false,
                            },
                        }],
                        xAxes: [{
                            ticks: {
                                fontColor: "rgba(255, 255, 255, 1)",
                            },
                            gridLines: {
                                color: "rgba(255, 255, 255, .2)",
                                borderDash: [5, 5],
                                zeroLineColor: "rgba(255, 255, 255, .2)",
                                zeroLineBorderDash: [5, 5]
                            },
                        }]
                    }
                }
            });
        }
    }
}
</script>

<!-- BUY ME A BEER AND HELP SUPPORT OPEN-SOURCE RESOURCES -->
<div class="flex items-end justify-end fixed bottom-0 right-0 mb-4 mr-4 z-10">
    <div>
        <a title="Buy me a beer" href="https://www.buymeacoffee.com/scottwindon" target="_blank" class="block w-16 h-16 rounded-full transition-all shadow hover:shadow-lg transform hover:scale-110 hover:rotate-12">
            <img class="object-cover object-center w-full h-full rounded-full" src="https://i.pinimg.com/originals/60/fd/e8/60fde811b6be57094e0abc69d9c2622a.jpg"/>
        </a>
    </div>
</div>

How to build a Stock Chart with AlpineJS and ChartsJS with Tailwind CSS?

Install tailwind css of verion 1.4.6

Use the link html tag to import the stylesheet of Tailwind CSS of the version 1.4.6

<link href=https://unpkg.com/[email protected]/dist/tailwind.min.css rel="stylesheet">

All the unility class needed to build a Stock Chart with AlpineJS and ChartsJS component

  • min-w-screen
  • min-h-screen
  • bg-gray-900
  • flex
  • px-5
  • py-5
  • overflow-hidden
  • w-full
  • md:flex
  • md:w-1/2
  • pb-4
  • pt-8
  • bg-indigo-500
  • text-white
  • p-10
  • bg-gray-100
  • text-gray-600
  • text-lg
  • text-gray-800
  • text-sm
  • mb-2
  • mb-6
  • block
  • text-3xl
  • ml-4
  • text-green-500
  • text-xs
  • w-5/12
  • flex-1
  • pr-3
  • text-left
  • px-3
  • text-right
  • w-7/12
  • pl-3
  • absolute
  • bottom-0
  • pb-2
  • text-gray-700
  • text-center
  • hover:text-gray-500
  • fixed
  • right-0
  • mb-4
  • mr-4
  • z-10
  • w-16
  • h-16
  • h-full

49 steps to build a Stock Chart with AlpineJS and ChartsJS component with Tailwind CSS

  1. Set the minimum width/height of an element using the min-w-screen utilities.

  2. Set the minimum width/height of an element using the min-h-screen utilities.

  3. Control the background color of an element to gray-900 using the bg-gray-900 utilities.

  4. Use flex to create a block-level flex container.

  5. Control the horizontal padding of an element to 1.25rem using the px-5 utilities.

  6. Control the vertical padding of an element to 1.25rem using the py-5 utilities.

  7. Use overflow-hidden to clip any content within an element that overflows the bounds of that element.

  8. Use w-full to set an element to a 100% based width.

  9. Use flex to create a block-level flex container at only medium screen sizes.

  10. Use md:w-1/2 to set an element to a fixed width(1/2) at only medium screen sizes.

  11. Control the padding on bottom side of an element to 1rem using the pb-4 utilities.

  12. Control the padding on top side of an element to 2rem using the pt-8 utilities.

  13. Control the background color of an element to indigo-500 using the bg-indigo-500 utilities.

  14. Control the text color of an element to white using the text-white utilities.

  15. Control the padding on all sides of an element to 2.5rem using the p-10 utilities.

  16. Control the background color of an element to gray-100 using the bg-gray-100 utilities.

  17. Control the text color of an element to gray-600 using the text-gray-600 utilities.

  18. Control the text color of an element to lg using the text-lg utilities.

  19. Control the text color of an element to gray-800 using the text-gray-800 utilities.

  20. Control the text color of an element to sm using the text-sm utilities.

  21. Control the margin on bottom side of an element to 0.5rem using the mb-2 utilities.

  22. Control the margin on bottom side of an element to 1.5rem using the mb-6 utilities.

  23. Use inline utilities to put the element on its own line and fill its parent.

  24. Control the text color of an element to 3xl using the text-3xl utilities.

  25. Control the margin on left side of an element to 1rem using the ml-4 utilities.

  26. Control the text color of an element to green-500 using the text-green-500 utilities.

  27. Control the text color of an element to xs using the text-xs utilities.

  28. Use w-5/12 to set an element to a fixed width(5/12).

  29. Use flex to create a block-level flex container.

  30. Control the padding on right side of an element to 0.75rem using the pr-3 utilities.

  31. Control the text color of an element to left using the text-left utilities.

  32. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

  33. Control the text color of an element to right using the text-right utilities.

  34. Use w-7/12 to set an element to a fixed width(7/12).

  35. Set the left padding of an element to 0.75rem using the pl-3 utilities class

  36. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  37. Use the bottom-0 utilities to set the bottom position of a positioned element to 0rem.

  38. Control the padding on bottom side of an element to 0.5rem using the pb-2 utilities.

  39. Control the text color of an element to gray-700 using the text-gray-700 utilities.

  40. Control the text color of an element to center using the text-center utilities.

  41. Control the text color of an element to gray-500 on hover using the hover:text-gray-500 utilities.

  42. Use fixed to position an element relative to the browser window.

  43. Use the right-0 utilities to set the right position of a positioned element to 0rem.

  44. Control the margin on bottom side of an element to 1rem using the mb-4 utilities.

  45. Control the margin on right side of an element to 1rem using the mr-4 utilities.

  46. Control the stack order (or three-dimensional positioning) of an element to 10 in Tailwind, regardless of order it has been displayed, using the z-10 utilities.

  47. Use w-16 to set an element to a fixed width(4rem).

  48. Use h-16 to set an element to a fixed height(4rem).

  49. Use h-full to set an element’s height to 100% of its parent, as long as the parent has a defined height.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to build a Stock Chart with AlpineJS and ChartsJS components, learn and follow along to implement your own components.