Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Programming

    23. Node.js Lessons. Domains, asynchronous try.. catch. Part 1.

    Node.js

    How to Upload Images and Videos to Cloudinary in Your Node.js Application: Step-by-Step Tutorial

    JavaScript

    Monthly Digest of the Most Popular and Trending JS GitHub Repos

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Thursday, December 18
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Programming / D3.js and Angular/Vue.js Integration
    Programming

    D3.js and Angular/Vue.js Integration

    Stepan ZharychevBy Stepan ZharychevNovember 29, 2017Updated:May 26, 2024No Comments6 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    D3.js and Angular/Vue.js Integration
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link

    Nowadays there’re lots of technologies in a Front-end and most of them have easily understandable tutorials. So starting with them is not a problem. But there’s always a problem when you try to use some of them together. In this article, we’re going to talk about a usage of Angular or Vue.js together with D3.js, and find out why we might have problems with the integration.

    When you integrate different technologies a very first question that you should resolve is defining of responsibilities. Each technology should do exactly the thing that it was brought for. D3 and MV frameworks integration is not an exception, but it has its own pitfalls.

    From the chart above we can understand clearly enough that both of our technologies get some data and represent it in the form of view. So basically they resolve particular the same problem in our case, and defining of responsibility zone for each can be a bit tricky.
    Let’s look behind and remember what is the framework and how it’s different from a library. The main difference here is that the framework dictates you the structure of a code, how components should interact, be defined and etc.
    While library just brings some module to be used in your project. So now we can do sort of flattening and decide who is in charge?

    What conclusions can we do after such flattening?

    • Framework is responsible for the entire architecture of application;
    • Framework is responsible for all the data flows of application (gets data and provides it to components);
    • D3.js is responsible only for SVG rendering with no ways to interact with data source directly as well as change it;

    In shorter words: D3.js logic is a part of a common component, it takes props with the data, does rendering and sends events back to main components/storages then there’s a need to mutate a data.

    Angular note

    For a long time in AngularJS it was a good practice to push everything that’s not connected with business logic into the services/factories, in the case of D3 rendering it can be an anti-pattern for you because of following reasons:

    • Logic of D3 handlers interaction with framework related stuff won’t be transparent enough being pushed into the service (because as you remember from time to timeyou
      want to send some commands into the other components);
    • Creation of generic service that will do the full diagram rendering is a generally bad idea (just remember I of SOLID principles);
    Read More:  Top 10 Bug Tracking Tools for Web Developers and Designers

    And just remember that the main purpose of services in Angular is to share logic between components. In our case it means one short rule:

    Many same looking diagrams in app – duplicate rendering parts go into the service.
    Otherwise stay in component.

    The question of integration is not a really outstanding thing, you just need to include rendering logic based on d3 into your component like this:

    private init() {
            this.svg = d3.select(this.$el)
                .select('svg');
    
            if (this.chart) {
                this.chart.remove();
            }
    
            this.chart = this.svg
                .append('g')
                .attr('transform', `translate(${constants.chart.padding}, ${constants.chart.padding})`);
    
            this.generateAxis();
            this.generateData();
    }

    And call this kind of method whenever you need a chart render (generateAxis and generateData are obviously methods that include corresponding logic). But this whenever can be a serious problem after all and to see where exactly we can meet a pitfall let’s examine the component structure of our test application. Our application is a simple Twitch streamer game popularity chart (shows a correlation between average online viewers of stream and game) with an ability to select exact game and see detailed info.

    As you can see our Chart component needs to communicate with Main component in 2 different ways:

    • Gets chart data prop and re-renders chart content whenever prop changed;
    • Sends selected chart entry info back to Main;

    While D3 is responsible for all the stuff like data-based axis drawing, scaling it to fit the window, positioning of all the required labels, line points calculation and etc. But if we use it for full rendering we need to track input props mutations fully by ourself, the framework has nothing to do with props anymore. But our todays guests have ways to workaround this situation.

    Read More:  How to Architect a Node.Js Project from Ground Up?

    In the case of Vue.js it’s really simple and straightforward solution – use watcher ( a function that will be called wherever property that’s bound to it is mutated):

    @Watch('data')
    private onDataChange() {
       this.data && this.init();
    }
    

    In the case of Angular we have 2 possible options:

    Simple one – use setter for @Input change handling.

    @Input()
    public set data(value: ChartEntry[]) {
     if (this._data && this._data !== value) {
       this.init();
     }
    
     this._data = value;
    }
    

    Hard one – use onChanges lifecycle hook, that takes every single change in all props of the component. This way is more preferable for the cases when a diagram is really large and you want to do partial re-rendering via tracking of exact entry change for example.

    Another thing that we need to handle is child-to-parent communication from d3.js handler, again Vue.js and Angular can do it without any problems.

    In the Vue case just use $emit on the parent level where it will be handled:

    this.chart.append('circle')
       .attr('cx', xScale(index + 1))
       .attr('cy', yScale(this.data[index].averageViewers))
       .attr('r', constants.chart.pointR)
       .on('click', () => {
           this.$parent.$emit('change-selected-index', index);
       })

    In the Angular case, we need to specify Output of component…

    @Output()
    public selectEntry = new EventEmitter<number>();

    … and use it to emit value back to the parent:

    this.chart.append('circle')
     .attr('cx', xScale(index + 1))
     .attr('cy', yScale(this.data[index].averageViewers))
     .attr('r', this.constants.chart.pointR)
     .on('click', () => {
       this.selectEntry.emit(index);
     })
    

    The code can be found in this repository and of course, you’re welcome to ask any questions in article comments!

    Instead of PS

    Always remember that D3.js is an instrument as well as any other and there’s no silver bullet in the programming. So if you see that your SVG is going to have 1-2 primitive elements inside and that’s it, then there’s no need to include D3. In such cases, you can easily render everything using framework abilities. You can find an appropriate example of such rendering inside Vue-primitive-svg folder of example repository.

    We are looking forward to meeting you on our website blog.soshace.com

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Stepan Zharychev
    • Website

    Related Posts

    Mastering REST APIs: Essential Techniques for Programmers

    December 18, 2024

    Crafting Interactive User Interfaces Using JavaScript Techniques

    December 17, 2024

    Effective Strategies for Utilizing Frameworks in Web Development

    December 16, 2024
    Leave A Reply Cancel Reply

    You must be logged in to post a comment.

    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    LinkedIn December 7, 2024

    Optimizing LinkedIn Lead Generation: Seamless CRM Integration

    Integrating LinkedIn with a CRM system enhances lead generation by streamlining data management and improving outreach efficiency. This seamless connection allows businesses to not only track leads effectively but also nurture relationships, ultimately driving conversions.

    2. Express.js Lessons. Logger, Configuration, Templating with EJS. Part 1.

    November 29, 2016

    Building a Telegram Bot with Node.js

    December 6, 2019

    The Concept of Scope in JavaScript

    November 13, 2019

    Categories

    • AI & Automation
    • Angular
    • ASP.NET
    • AWS
    • B2B Leads
    • Beginners
    • Blogs
    • Business Growth
    • Case Studies
    • Comics
    • Consultation
    • Content & Leadership
    • CSS
    • Development
    • Django
    • E-commerce & Retail
    • Entrepreneurs
    • Entrepreneurship
    • Events
    • Express.js
    • Facebook Ads
    • Finance & Fintech
    • Flask
    • Flutter
    • Franchising
    • Funnel Strategy
    • Git
    • GraphQL
    • Home Services Marketing
    • Influencer & Community
    • Interview
    • Java
    • Java Spring
    • JavaScript
    • Job
    • Laravel
    • Lead Generation
    • Legal & Compliance
    • LinkedIn
    • Machine Learning
    • Marketing Trends
    • Medical Marketing
    • MSP Lead Generation
    • MSP Marketing
    • NestJS
    • Next.js
    • Node.js
    • Node.js Lessons
    • Paid Advertising
    • PHP
    • Podcasts
    • POS Tutorial
    • Programming
    • Programming
    • Python
    • React
    • React Lessons
    • React Native
    • React Native Lessons
    • Recruitment
    • Remote Job
    • SaaS & Tech
    • SEO & Analytics
    • Soshace
    • Startups
    • Swarm Intelligence
    • Tips
    • Trends
    • Vue
    • Wiki
    • WordPress
    Top Posts

    Hunting Down and Fixing Memory Leaks in Java

    Java January 5, 2020

    Strategic Partnerships: A Key to Scaling Your Business Effectively

    Entrepreneurship December 4, 2024

    An Introduction to Finite State Machines: Simplifying React State Management with State Machines

    JavaScript February 2, 2021

    Working the Agile Way: Lean and Kanban

    Beginners May 29, 2019

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    About Us
    About Us

    Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

    7901 4th St N, Suite 28690
    Saint Petersburg, FL 33702-4305
    Phone: 1(877)SOSHACE

    Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
    Our Picks
    Wiki

    Заметка про распорядок дня

    LinkedIn

    Effective LinkedIn Outreach: Proven Strategies for Prospects

    JavaScript

    Create simple POS with React, Node and MongoDB #3: setup E-mail pipeline with add activate on SignUp

    Most Popular

    How to Upload Images and Videos to Cloudinary in Your Node.js Application: Step-by-Step Tutorial

    Node.js

    Interview with Alex

    Interview

    JAMstack Architecture with Next.js

    JavaScript
    © 2025 Soshace Digital.
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions

    Type above and press Enter to search. Press Esc to cancel.