投稿時間:2023-06-05 08:14:54 RSSフィード2023-06-05 08:00 分まとめ(16件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
IT 気になる、記になる… 「Bing」のAIチャット、「Safari」や「Chrome」のサポートに向けテスト開始 https://taisy0.com/2023/06/05/172514.html apple 2023-06-04 22:51:35
AWS AWSタグが付けられた新着投稿 - Qiita vCenter Converter で EC2 インスタンスを VMware Cloud on AWS に移行する https://qiita.com/mtoyoda/items/a2a0c96196c58d86e14a vcenterco 2023-06-05 07:48:23
海外TECH DEV Community How to visualize data with a bar chart using d3 https://dev.to/ifeolwaisaiah/how-to-visualize-data-with-a-bar-chart-using-d3-1m8e How to visualize data with a bar chart using dMaybe it just occurred to you that you haven t visualized any data before since the start of your coding journey or perhaps you haven t found a good blog explaining how data can be visualized Say no more In this blog post we are going through the process of displaying data with a bar chart using my freeCodeCamp data visualization project as a real life demo We would also be using a javaScript library called D js for those unfamiliar with D it is a JavaScript library for producing dynamic interactive data visualizations in web browsers The first step is to add the D script tag in the head of your html lt script src gt lt script gt With that added congrats we can now visualize data I am keeping in mind that d can be a very confusing thing to grasp at first sight which is why I am going to break it down into much smaller bit so you understand why we do what we do The HTML Add an svg element in your html This can be done manually by adding the following line of code lt svg id chart gt lt svg gt Your svg id be anything you like as long as it relates to what is being done Now adding an svg doesn t do anything but hold the rest of the element we are going to put in it later Before moving on to the javascript aspect our bar chart needs an heading Add a text element we can again just include this with a text tag directly in our svg add the line below in the svg tag lt text id title x y gt United States GDP lt text gt Not a fan of Maths Don t be confused the x and y above only helps with the positioning as it doesn t behave like a normal html element x is the left and y is the top think of it as a margin left and margin top In summary our html file should look similar to this lt DOCTYPE html gt lt html lang en gt lt head gt lt meta charset UTF gt lt meta name viewport content width device width initial scale gt lt title gt bar chart lt title gt lt script src gt lt script gt lt head gt lt body gt lt svg id canvas gt lt text id title x y gt United States GDP lt text gt lt svg gt lt body gt lt html gt Some CSSIf you aren t ready to give your bar chart some styling I suggest giving the svg at least a background color so you can see the changes that will be made for this project I have a few styles added you can customize this to suit your taste body width height vh margin padding display flex flex direction column justify content center align items center background color ae font family Arial Helvetica sans serif svg background color white border radius px padding px title font size px tooltip margin top px font size px color white rect fill ae rect hover fill fee With html and css out of the way lets get to why you are here The JavaScript Defining some variables In your script we are going to define the variables that are going to be used later in the creation of our bar chart add the code below in your scriptlet values let heightScale let xScale let xAxisScale let yAxisScale let width let height let padding The values array is empty because later on it s going to contain the data we are going to get from an api The heightScale is what will determine the height of each of the bars as it cannot have say a constant value like or if that were so then all bars will have the same height irrespective of the value it represents The xScale will determine where the bars are placed horizontally in the chart The xAxisScale will be use to create the xAxis at the bottom while the yAxisScale will be used to create to the yAxis on the left you can think of it as the x and y axis on a graph The width height and padding are attributes that are going to be added to the svg Select the svg To select an element in normal javascript we usually go through the route of document getElementById or document querySelector or whatnot but because we are using d here we can just do thislet svg d select svg and that s it you have selected the svg element in your html and saved it in a variable called svg if there are more than one svg in the document then it will return the first element you can also select by the id or class of the element for examplelet svg d select chart both are valid ways of selecting an element in d Create functions Now that we have our variables set we need to create some functions that will be called to do one thing or another in order to create our bar chart I Let s drawChart The svg is where all other element are going to sit like the xAxis yAxis and the bars For that we need to define the width and height of it using a function Add the code below after selecting your svg let drawChart gt svg attr width width attr height height The above code is a simple arrow function that takes the svg variable and adds an attribute to it The d attr takes two argument first is the attribute we want to add which in this case is the width and height and second is the value II Let s fetch some data Before we proceed creating the bar chart we need to fetch the data that the bar chart will be created with now freeCodeCamp gave us an api to work with You can open this up in a new tab to see how the data is structured To fetch this data I am going simply use javaScript s own fetch method add the below code after the drawChart fetch get the data then res gt res json convert the data to json then data gt values data data save the data in the values array console log values just so you can see that values now contains the data The code above simply gets the data and stores them in the values array if you check your console you should see thisLastly within the fetch method we want to call the functions we ll be creating later add the code below to your fetchdrawChart generateScales drawBars generateAxes You should have something like this fetch then res gt res json then data gt values data data console log values drawChart generateScales drawBars generateAxes III Let s generateScales We need to create the scales that the axis are going to use add the below code after your drawChart let generateScales gt heightScale d scaleLinear domain d max values item gt return item range height padding xScale d scaleLinear domain values length range padding width padding First we defined the heightScale by calling d scaleLinear function after that we had to call a domain a domain is simply telling it that the values we are expecting here have a maximum and minimum take the code below for an exampleconst arr In the array above the minimum number present is and the maximum is That is essentially what the domain does it gets the min and max of the data But the domain can t magically know the min and max of the data we have to tell it by opening an array in it and specifying the lowest value as the first item and highest value as the second domain Since we are working with GDP data we know for sure that the lowest can only be else we d have worked with d min to find the lowest value in the array Now that the min of the domain is set we have to find the max as the second item in the domain array now in real life one cannot always know the exact figure and it maybe subject to change in the future that s why the below code is added as the second item in the array it takes the values array and maps over each item before returning the highest value of the second index d max values item gt return item That s how we have this domain d max values item gt return item Now to the range it also takes in an array with two values the first is the min then the max take the code below for an example our dataconst arr If you think about it how can one represent a huge value like should the height then be px tall Then what happens when we work in thousands or millions how to be present the height of that bar representing the value That is what the range doesconst arr we can say to represent this data the range can be range What this does is that the value of will be represented at the height of because it is the min and will be represented at the height of because it is the max likewise the rest of the value s height will be calculated within the range we have set for it I hope this code now makes more sense to you range height padding It sets the min range to and the max to the height of the svg take away the padding on both sides so there s a bit of a space Then we defined our xScale similarlyxScale d scaleLinear domain values length range padding width padding This scale is used to position the xAxis which would sit horizontally at the bottom Moving on in the generateScale right after the xScale add the following code let datesArr values map item gt return new Date item console log datesArr so you can see xAxisScale d scaleTime domain d min datesArr d max datesArr range padding width padding yAxisScale d scaleLinear domain d max values item gt return item range height padding padding Now in order to have this bottom axis paired with dates belowWe need to convert the string date from our data and change it to an actual date if you studied the data you will see that the date for each value is at the index of For this conversion to happen we created a datesArr to hold the newly converted dates let datesArr values map item gt return new Date item If you console log datesArr you ll find our array containing the newly converted datesAfter that we defined the xAxisScale by calling the d scaleTime because the values we are working with are dates then we called the domain and range likewise for the yAxisScale IV Let s generateAxes Now that the hard part which is generateScales is out of the way it s time to start with our both of our axis add this code below the generateScales function let generateAxes gt let xAxis d axisBottom xAxisScale svg append g call xAxis attr id x axis attr transform translate height padding let yAxis d axisLeft yAxisScale svg append g call yAxis attr id y axis attr transform translate padding Within the function above we defined an xAxis representing the x of our graph before calling it with the d axisBottom xAxisScale method and giving it the xAxisScale Now to display this on screen a g element is appended the already selected svg then we called the xAxis which is essentially telling it to draw the xAxis within the g let xAxis d axisBottom xAxisScale svg append g call xAxis attr id x axis attr transform translate height padding An id attribute is added to the g and a transform attribute which if not added would naturally sit at the top of the svg try taking away the transform attribute and you will have something like thisAfter that the yScale is defined and called with the d axisLeft just like we did for the xAxis another g is appended to the svg then we told it to draw the yAxis within it then an id attribute is given to it and a transform attribute to position it better within the svg try taking the transform out to see its natural position let yAxis d axisLeft yAxisScale svg append g call yAxis attr id y axis attr transform translate padding So in total we have something like this V Let s drawBars Finally we are going to be filling the graph with bars representing its data add the code below after the generateScales let drawBars gt svg selectAll rect data values enter append rect attr class bar attr width width padding values length attr data date item gt return item attr data gdp item gt return item attr height item gt return heightScale item attr x item index gt return xScale index attr y item gt return height padding heightScale item Breaking it down svg selectAll rect data values enter append rect First we selected all rect or rectangle within the svg whether they exist or not then we bind the rect with values with data values to associate each rect with each value then the enter is called to tell it what to do if no rectangles are found and right after that the append rect is called which would create new rectangle for each value Then we added some attributes like width height class x and y for proper display and positioning of each bar other attributes like data date and data gdp are for the fulfillment of freeCodeCamp s test but it s good practice to add them too Again try taking away the x or y attribute to see how they would be positioned by default attr class bar attr width width padding values length attr data date item gt return item attr data gdp item gt return item attr height item gt return heightScale item attr x item index gt return xScale index attr y item gt return height padding heightScale item You should have something similar to this now VI The Tooltip Now that we have our bar chart displayed the last thing is to add a tooltip to display some information about the particular bar hovered on now for this I have created a very simple tooltip add the code below at the top of your drawBars before the rect selection let tooltip d select body append div attr id tooltip style visibility hidden style width auto style height auto What this does is to select the body add a div to it and give it an attribute of id which is set to tooltip right after that we gave a few styles to it like setting the visibility to hidden by default setting the width and height to auto Now that we have our tooltip the next thing is displaying the necessary information when hovered upon for this d has a method for us which is the on add the code below right after the y attribute of the svg think of it as adding an event listener to the bars on mouseover item index gt tooltip style visibility visible html Date index Data lt b gt index lt b gt attr data date index on mouseout item gt tooltip style visibility hidden Using the on method the first argument takes the name of the the event while the second is the function that should run when the event happens this function takes the item and the index of the item Inside the function the tooltip visibility style is set back to visible when the mouseover happens and is given some html which include the date and value of the particular bar hovered upon a data date attribute is also given to the tooltip whose value is the date of the bar hovered upon on mouseover item index gt tooltip style visibility visible html Date index Data lt b gt index lt b gt attr data date index Now for when the mouse leaves we have this line of code below which sets the visibility of the tooltip back to hidden that way it toggles on and off based on the event happening on mouseout item gt tooltip style visibility hidden The Full codelet values let heightScale let xScale let xAxisScale let yAxisScale let width let height let padding let svg d select canvas the drawChart functionlet drawChart gt svg attr width width attr height height the generateScales functionlet generateScales gt heightScale d scaleLinear domain d max values item gt return item range height padding xScale d scaleLinear domain values length range padding width padding let datesArr values map item gt return new Date item console log datesArr xAxisScale d scaleTime domain d min datesArr d max datesArr range padding width padding yAxisScale d scaleLinear domain d max values item gt return item range height padding padding the drawBars function let drawBars gt let tooltip d select body append div attr id tooltip style visibility hidden style width auto style height auto svg selectAll rect data values enter append rect attr class bar attr width width padding values length attr data date item gt return item attr data gdp item gt return item attr height item gt return heightScale item attr x item index gt return xScale index attr y item gt return height padding heightScale item on mouseover item index gt tooltip style visibility visible html Date index Data lt b gt index lt b gt attr data date index on mouseout item gt tooltip style visibility hidden the generateAxes function let generateAxes gt let xAxis d axisBottom xAxisScale svg append g call xAxis attr id x axis attr transform translate height padding let yAxis d axisLeft yAxisScale svg append g call yAxis attr id y axis attr transform translate padding fetching the data fetch then res gt res json then data gt values data data console log values drawChart generateScales drawBars generateAxes ConclusionCongratulations You have just created a bar chart The code above shows how HTML SVG CSS and JavaScript can work together to create a stunning data graph Understanding how each of the different elements works and their functions is crucial for developing more complex data graphs if you want to know more about d there are many tutorials and online resources that can have you have better understanding of it This can be a lot to grasp but don t be too discouraged if you want me to make a detailed tutorial about d all you need to do is comment down below Thanks for reading 2023-06-04 22:31:29
Apple AppleInsider - Frontpage News Apple Back to School deals could start on Tuesday https://appleinsider.com/articles/23/06/04/apple-back-to-school-deals-could-start-on-tuesday?utm_medium=rss Apple Back to School deals could start on TuesdayApple s Back to School promotion could start this week in the United States a leaker claims with a possible launch of the sales event on Tuesday following the launch of new Mac models Some of the Apple products from Apple s Back to School dealsThe annual Back to School promotion typically takes place early in the summer but it seems that for it may be earlier than last year It is claimed that the educational sales event could take place on Tuesday June Read more 2023-06-04 22:19:04
海外TECH CodeProject Latest Articles Perceptual Hash based Image Comparison: Coded in "plain old C". An Anecdotal View https://www.codeproject.com/Articles/5362105/Perceptual-Hash-based-Image-Comparison-Coded-in-pl Perceptual Hash based Image Comparison Coded in quot plain old C quot An Anecdotal ViewAn anecdotal report describing issues encountered in coding a minimal perceptual hash capability in plain old C in to year old rolling development with the goal of running on a noob Win installation 2023-06-04 22:53:00
金融 ニュース - 保険市場TIMES 三井住友海上、中堅・中小企業向け情報提供サイト「MSコンパス」を開設 https://www.hokende.com/news/blog/entry/2023/06/05/080000 三井住友海上、中堅・中小企業向け情報提供サイト「MSコンパス」を開設経営課題の解決に役立つ情報やサービスを提供三井住友海上火災保険株式会社以下、三井住友海上は年月日、中堅・中小企業向けの情報提供サイト、「MSコンパスMiraiSolutionCompass」を開設したことを発表した。 2023-06-05 08:00:00
ニュース BBC News - Home Oil production to drop after Opec+ nations meet https://www.bbc.co.uk/news/business-65804768?at_medium=RSS&at_campaign=KARANGA russia 2023-06-04 22:37:22
ニュース BBC News - Home Zlatan Ibrahimovic retires: Swedish great ends football career at 41 https://www.bbc.co.uk/sport/football/65806224?at_medium=RSS&at_campaign=KARANGA final 2023-06-04 22:52:09
ビジネス ダイヤモンド・オンライン - 新着記事 ツイッター次期CEO、元NBCU幹部を部下に起用 - WSJ発 https://diamond.jp/articles/-/324001 部下 2023-06-05 07:18:00
ビジネス ダイヤモンド・オンライン - 新着記事 米中が非難応酬 台湾海峡で軍艦接近も - WSJ発 https://diamond.jp/articles/-/324002 台湾海峡 2023-06-05 07:06:00
ビジネス 東洋経済オンライン 重力に逆らって上昇する日経平均の下落はいつか 世界の投資環境はどんどん悪くなっている | 市場観測 | 東洋経済オンライン https://toyokeizai.net/articles/-/677309?utm_source=rss&utm_medium=http&utm_campaign=link_back 日経平均 2023-06-05 07:30:00
マーケティング MarkeZine 約8割が値上げで生活に影響があった 一番困ったものは生鮮食品と約7割が回答【LENDEX調査】 http://markezine.jp/article/detail/42403 lendex 2023-06-05 07:30:00
仮想通貨 BITPRESS(ビットプレス) [日経] 仮想通貨マネロン対策、新ルール始動 対象は業者間のみ https://bitpress.jp/count2/3_9_13631 新ルール 2023-06-05 07:11:53
ニュース THE BRIDGE Adobeが画像ジェネレーティブAI「Firefly」をβ公開、Photoshop上で画像をテキストで操作可能に https://thebridge.jp/2023/06/adobe-integrates-generative-ai-directly-into-photoshop-with-new-firefly-capabilities Adobeが画像ジェネレーティブAI「Firefly」をβ公開、Photoshop上で画像をテキストで操作可能にAdobeは初めて、主力製品であるPhotoshopにジェネレーティブAIを連携した。 2023-06-04 22:45:38
ニュース THE BRIDGE 台湾のスタートアップカンファレンス「InnoVEX 2023」が開催、ピッチコンテストのファイナリスト10社をご紹介 https://thebridge.jp/2023/06/innovex-2023-pitch-contest-finalists-showcased 台湾のスタートアップカンファレンス「InnoVEX」が開催、ピッチコンテストのファイナリスト社をご紹介現金、ネットワーキングの機会、専門的な起業家向けサービスは、すべての起業家チームが夢見ることのできるリソースだ。 2023-06-04 22:30:27
ニュース THE BRIDGE 阿里巴巴がLLM搭載「通義聴悟」発表、百度がLLM企業向け200億円ファンド設立——中国スタートアップシーン週間振り返り(5月29日~6月2日) https://thebridge.jp/2023/06/technode-may-29-jun-2 阿里巴巴がLLM搭載「通義聴悟」発表、百度がLLM企業向け億円ファンド設立ー中国スタートアップシーン週間振り返り月日月日本稿は、Technode動点科技が、月日月日に配信した「NewsFeed」記事の中から主要ニュースを翻訳したものです。 2023-06-04 22:15:18

コメント

このブログの人気の投稿

投稿時間:2021-06-17 22:08:45 RSSフィード2021-06-17 22:00 分まとめ(2089件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)